我有一个类可能被多个客户端(即 GUI、外部系统等)使用。
@XmlRootElement
class A {
String field = "Hello World";
public String getField() {
return field;
}
}
对于一个客户端,我不希望生成的 XML 包含类中的所有详细信息,因此我使用 XmlTransient 标记要隐藏的详细信息。
@XmlRootElement
class A {
String field = "Hello World";
@XmlTransient
public String getField() {
return field;
}
}
问题是其他客户端需要查看该数据,但现在它被隐藏了。
如果我有两个客户想要几乎相同的数据,我该如何满足这个要求?我通过扩展 A 并适当注释来满足每个客户的需求来完成这项工作:
@XmlRootElement
class B extends A {
@XmlTransient
public String getField() {
return super.getField();
}
}
@XmlRootElement
class C extends A {
public String getField() {
return super.getField();
}
}
这可行但看起来很麻烦。还有其他方法可以做到这一点吗?我意识到,在我展示的实例中,我可以使用 XmlJavaAdapter,但在我的实际代码中,我引用的数据可能是复合对象的一部分,而 XmlJavaAdapter 似乎不合适。
希望这一切都有道理!
I have a class that may be consumed by multiple clients (i.e a GUI, an external system etc).
@XmlRootElement
class A {
String field = "Hello World";
public String getField() {
return field;
}
}
For one client I don't want the resulting XML to include all of the details in the class so I mark the details I want to hide with XmlTransient.
@XmlRootElement
class A {
String field = "Hello World";
@XmlTransient
public String getField() {
return field;
}
}
The trouble is that the other client needs to see that data, and now it's hidden.
How do I go about fulfilling a requirement where I have two clients who want almost the same data? I've made this work by extending A and annotating appropriately to meet the needs of each client:
@XmlRootElement
class B extends A {
@XmlTransient
public String getField() {
return super.getField();
}
}
@XmlRootElement
class C extends A {
public String getField() {
return super.getField();
}
}
This works but seems cumbersome. Are there any other ways of doing this? I realise that in the instance I have shown I could use an XmlJavaAdapter but in my real-life code the data I am referring to could be part of a composite object and the XmlJavaAdapter seems inappropriate.
Hope this all makes sense!
发布评论
评论(2)
注意:我是EclipseLink JAXB (MOXy)< /strong> JAXB 的领导者和成员(JSR-222)专家组。
正如 @skaffman 提到的,您可以使用 MOXy 扩展来完成此用例。 MOXy 能够将映射元数据表示为 XML。这意味着您可以提供一个作为注释的映射,以及作为 XML 的附加映射。
下面是一个示例的链接,其中单个对象模型映射到 Google 和 Yahoo 天气服务的结果:
与此示例相对应的代码也可以在 GitHub 上找到:
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
As mentioned by @skaffman, you can use MOXy extensions to accomplish this use case. MOXy offers the ability to represent the mapping metadata as XML. This means that you can provide one mapping as annotations, plus additional mappings as XML.
Below is a link to an example where a single object model is mapped to the results of the Google and Yahoo weather services:
The code that corresponds to this example is also available on GitHub:
我猜 MOXy 不是免费的(如果我错了请纠正我)。
我的免费解决方案是在 JAXB 对象中使用额外的“版本”字段来区分绑定的多个版本。通常,我使用 enum 作为其类型,即
枚举版本{V1,V2,...};
因此,对于原始示例,我将 getField() 方法定义为
对于客户端 1,我将版本值设置为 Version.V1,然后编组 Java 对象;对于客户端 2 也是如此,我将在编组之前将版本值更改为 Version.v2。
I guess MOXy is not free (correct me if I am wrong please).
My free solution is to use an extra "version" field in the JAXB object to distinguish multiple versions of bindings. Usually, I use enum as its type, i.e.,
enum Version {V1, V2, ...};
So for the original example, I define the getField() method as
For client 1, I will set the version value to Version.V1, and then marshall the Java object; same for client 2, I will change the version value to Version.v2 before marshalling.