java JAXB 集合 (List<T>) 对元素名称使用 Pascal Case 而不是 Camel Case
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1545041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAXB Collections (List<T>) Use Pascal Case instead of Camel Case for Element Names
提问by jallch
I have a number of JAXB beans that are directly marshalled and unmarshalled using Jersey.
我有许多使用 Jersey 直接编组和解组的 JAXB bean。
E.g.
例如
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Artifact", propOrder = {
"artifactId",
"artifactType",
"contentHash"
})
@XmlSeeAlso({
EmailArtifact.class,
ManagedDocArtifact.class
})
@XmlRootElement(name ="Artifact")
public class Artifact {
protected String artifactId;
protected String artifactType;
protected String contentHash;
...
...
...
}
If I create a GET method that returns a single artifact object. It correctly produces the XML:
如果我创建一个返回单个工件对象的 GET 方法。它正确地生成了 XML:
<Artifact>
<artifactId>293289392839283</artifactId>
<artifactType>EMAIL</artifactType>
<contentHash>2837873827322</contentHash>
</Artifact>
Here I have been able to successfully control the name of the Artifact element to have a capital "A" at the beginning.
在这里,我已经能够成功控制 Artifact 元素的名称以大写字母“A”开头。
However, I create a GET method that returns collection of artifact objects, I end up with:
但是,我创建了一个返回工件对象集合的 GET 方法,我最终得到:
<artifacts>
<Artifact>
<artifactId>293289392839283</artifactId>
<artifactType>EMAIL</artifactType>
<contentHash>2837873827322</contentHash>
</Artifact>
<Artifact>
<artifactId>293289392839283</artifactId>
<artifactType>EMAIL</artifactType>
<contentHash>2837873827322</contentHash>
</Artifact>
</artifacts>
As you can see the outer element for the collection has a lower case "A". In order to conform to our own internal standard I would like this to be a capital "A" - Artifacts.
正如您所看到的,集合的外部元素有一个小写的“A”。为了符合我们自己的内部标准,我希望这是一个大写的“A”——Artifacts。
I can not see where this is possible to define within JAXB, is it actually the Jersey framework that is controlling this?
我看不到可以在 JAXB 中定义的位置,它实际上是控制它的 Jersey 框架吗?
Can we control of the element name generated for collections?
我们可以控制为集合生成的元素名称吗?
Many thanks,
非常感谢,
James
詹姆士
回答by java.is.for.desktop
Try using @XmlElementWrapperwhich supports the nameattribute:
尝试使用支持该属性的@XmlElementWrappername:
@XmlElementWrapper(name="ELEMENTS")
@XmlElement(name="ELEMENT")
protected final List<String> elements = new LinkedList<String>();
Will produce
会产生
<ELEMENTS>
<ELEMENT>one</ELEMENT>
<ELEMENT>two</ELEMENT>
...
</ELEMENTS>
See the JAXB Tutorialfor this feature.
有关此功能,请参阅JAXB 教程。
回答by jallch
OK - I have had an update from the Java Jersey community leader - Paul Sandoz. This is currently an open issue with the Jersey framework and just something that we will have to workaround or accept until it is fixed in a future update.
好的 - 我收到了来自 Java Jersey 社区领袖 - Paul Sandoz 的更新。这目前是 Jersey 框架的一个未决问题,只是我们必须解决或接受的问题,直到它在未来的更新中得到修复。
The workaround as explained in one of the comments above is to introduce a new class for each web service method that returns a collection (e.g. List) of JAXB annotated beans. This class is effectively a class that contains just one member - a List of the JAXB beans. Instead of returning the List in the web service method we return the special class instead. Because we have control over the name of the new class in the XML, e.g. @XmlRootElement(name="Artifacts") we can workaround the problem in the short term.
上述评论之一中解释的解决方法是为每个返回 JAXB 注释 bean 集合(例如列表)的 Web 服务方法引入一个新类。这个类实际上是一个只包含一个成员的类 - JAXB bean 的列表。我们没有返回 Web 服务方法中的 List,而是返回特殊的类。因为我们可以控制 XML 中新类的名称,例如 @XmlRootElement(name="Artifacts") 我们可以在短期内解决这个问题。
Hope this helps.
希望这可以帮助。
Regards,
问候,
James
詹姆士

