【问题标题】:JAXB: easy way to annotate name of element tags for object listsJAXB:为对象列表注释元素标签名称的简单方法
【发布时间】:2016-11-29 08:01:05
【问题描述】:

我正在寻找一种方法,以便在通过 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 的名称。

作为一个附带问题,除了让人类更容易阅读列表的元素名称之外,这个问题真的很重要吗?

感谢您调查这个问题。任何帮助表示赞赏。我在网上查看了类似的问题,但似乎没有什么完全适合我的问题。

问候

【问题讨论】:

    标签: java jaxb jersey jax-rs xml-serialization


    【解决方案1】:

    作为already stated here:

    您不能仅使用 JAXB 来实现这一点,因为注释必须是 穿上ArrayList 类本身。

    解决方案是使用 JacksonObjectWriter 的方法: withRootName()


    但是,我认为为此创建自己的包装类要简单得多,因为 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>
    

    【讨论】:

    • 您好,感谢您的回复。这就是我最终做的事情。您分享的链接对未来的 JAXB 努力者很有帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 2017-11-16
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多