JAXB:注释对象列表元素标签名称的简便方法
JAXB: easy way to annotate name of element tags for object lists
我正在寻找一种方法,以便在通过 Web 服务发布对象时使用 JAXB 轻松更改对象列表的元素名称。这是示例:
说我有一个对象我想序列化到 XML:
@XmlRootElement(name = "greeting")
public class TestClassForSo {
public String id;
public List<String> description;
}
我有一个 Web 服务,将这些对象的列表发布为 XML:
@GET
@Path("/so-test")
@Produces({AgiMediaType.APPLICATION_XML, AgiMediaType.APPLICATION_JSON})
@XmlElementWrapper(name = "wrapperName")
public List<TestClassForSo> getGreetings() {
List<TestClassForSo> greetingList = new ArrayList<>();
TestClassForSo greeting = new TestClassForSo();
String id = "hello-word";
List<String> greetings = new ArrayList<>(Arrays.asList("Hello World,
Bonjour le monde,
Hallo Welt,
Hola Mundo"));
greeting.id = id;
greeting.description = greetings;
greetingList.add(greeting);
return greetingList;
}
当我通过 HTTP GET 调用请求资源时得到以下结果:
<testClassForSoes>
<greeting>
<id>hello-word</id>
<description>Hello World, Bonjour le monde, Hallo Welt, Hola Mundo</description>
</greeting>
</testClassForSoes>
除了将其放入包装器方法或编写我自己的序列化机制等之外,是否有一种简单的方法可以将列表的元素名称更改为 greetings
?
显然,由于命名约定,我无法更改 TestClassForSo
的名称。
作为附带问题,这个问题除了使列表的元素名称对人类更易读之外真的很重要吗?
感谢您调查问题。任何帮助表示赞赏。我在网上调查了类似的问题,但似乎没有什么能完全符合我的问题。
问候
作为already stated here:
You can't achieve this with only JAXB, since the annotation would have to be
put on the ArrayList
class itself.
The solution would be with the use of Jackson ObjectWriter
's method:
withRootName()
但是,我认为为此创建自己的包装器class要简单得多,因为 Jersey 不允许根名称自定义:
@XmlRootElement(name = "greetings")
public class RootClass {
@XmlElement(name = "greeting")
public List<TestClassForSo> greetingList;
}
并将您的方法策略更改为以下内容,例如:
@GET
@Path("/so-test")
@Produces({AgiMediaType.APPLICATION_XML, AgiMediaType.APPLICATION_JSON})
public RootClass getGreetings() {
List<TestClassForSo> greetingList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
TestClassForSo testClassForSo = new TestClassForSo();
String id = "hello-word";
List<String> greetings = new ArrayList<>(Arrays.asList("Hello World " + i));
testClassForSo.id = id;
testClassForSo.description = greetings;
greetingList.add(testClassForSo);
}
RootClass r = new RootClass();
r.greetingList = greetingList;
return r;
}
那么,我们就有了最终的结果:
<greetings>
<greeting>
<id>hello-word</id>
<description>Hello World 0</description>
</greeting>
<greeting>
<id>hello-word</id>
<description>Hello World 1</description>
</greeting>
<greeting>
<id>hello-word</id>
<description>Hello World 2</description>
</greeting>
</greetings>
我正在寻找一种方法,以便在通过 Web 服务发布对象时使用 JAXB 轻松更改对象列表的元素名称。这是示例:
说我有一个对象我想序列化到 XML:
@XmlRootElement(name = "greeting")
public class TestClassForSo {
public String id;
public List<String> description;
}
我有一个 Web 服务,将这些对象的列表发布为 XML:
@GET
@Path("/so-test")
@Produces({AgiMediaType.APPLICATION_XML, AgiMediaType.APPLICATION_JSON})
@XmlElementWrapper(name = "wrapperName")
public List<TestClassForSo> getGreetings() {
List<TestClassForSo> greetingList = new ArrayList<>();
TestClassForSo greeting = new TestClassForSo();
String id = "hello-word";
List<String> greetings = new ArrayList<>(Arrays.asList("Hello World,
Bonjour le monde,
Hallo Welt,
Hola Mundo"));
greeting.id = id;
greeting.description = greetings;
greetingList.add(greeting);
return greetingList;
}
当我通过 HTTP GET 调用请求资源时得到以下结果:
<testClassForSoes>
<greeting>
<id>hello-word</id>
<description>Hello World, Bonjour le monde, Hallo Welt, Hola Mundo</description>
</greeting>
</testClassForSoes>
除了将其放入包装器方法或编写我自己的序列化机制等之外,是否有一种简单的方法可以将列表的元素名称更改为 greetings
?
显然,由于命名约定,我无法更改 TestClassForSo
的名称。
作为附带问题,这个问题除了使列表的元素名称对人类更易读之外真的很重要吗?
感谢您调查问题。任何帮助表示赞赏。我在网上调查了类似的问题,但似乎没有什么能完全符合我的问题。
问候
作为already stated here:
You can't achieve this with only JAXB, since the annotation would have to be put on the
ArrayList
class itself.The solution would be with the use of Jackson
ObjectWriter
's method:withRootName()
但是,我认为为此创建自己的包装器class要简单得多,因为 Jersey 不允许根名称自定义:
@XmlRootElement(name = "greetings")
public class RootClass {
@XmlElement(name = "greeting")
public List<TestClassForSo> greetingList;
}
并将您的方法策略更改为以下内容,例如:
@GET
@Path("/so-test")
@Produces({AgiMediaType.APPLICATION_XML, AgiMediaType.APPLICATION_JSON})
public RootClass getGreetings() {
List<TestClassForSo> greetingList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
TestClassForSo testClassForSo = new TestClassForSo();
String id = "hello-word";
List<String> greetings = new ArrayList<>(Arrays.asList("Hello World " + i));
testClassForSo.id = id;
testClassForSo.description = greetings;
greetingList.add(testClassForSo);
}
RootClass r = new RootClass();
r.greetingList = greetingList;
return r;
}
那么,我们就有了最终的结果:
<greetings>
<greeting>
<id>hello-word</id>
<description>Hello World 0</description>
</greeting>
<greeting>
<id>hello-word</id>
<description>Hello World 1</description>
</greeting>
<greeting>
<id>hello-word</id>
<description>Hello World 2</description>
</greeting>
</greetings>