使用Jersey生成JSON和XML时,@ XmlElement并不总是有效

时间:2018-09-06 15:28:34

标签: java tomcat jackson jersey json-serialization

我要根据Accept标头将POJO序列化为JSON和XML。

@Path("/json/metallica")
public class JSONService{

    @POST
    @Path("/post")
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response getTrackInJSON() {
        Track track = new Track();
        track.setTitle("Enter Sandman");
        track.setSinger("Metallica");
        return Response.status(201).entity(track).build();
    }

}

我想更改XML标记的名称以及JSON密钥名称,为此我使用了@XmlElement,因为它似乎可以同时用于XML和JSON。

看我想序列化为XML和JSON的POJO类,问题在于,在Tomcat中重新部署.war之后,@ XmlElement在序列化为JSON时并不总是起作用。一半时间将声明的名称放在@XmlElement(name =“”)中,另一半则保留字段的名称。

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Track {

    private String title;

    private String singer;

    public String getTitle() {
        return title;
    }

    @XmlElement(name = "title_element")
    public void setTitle(String title) {
        this.title = title;
    }

    public String getSinger() {
        return singer;
    }

    @XmlElement(name = "singer_element")
    public void setSinger(String singer) {
        this.singer = singer;
    }

    @Override
    public String toString() {
        return "Track [title=" + title + ", singer=" + singer + "]";
    }

}

XML格式的结果始终正确为<singer_element><title_element>

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<track>
    <singer_element>Metallica</singer_element>
    <title_element>Enter Sandman</title_element>
</track>

但是对于JSON,有时会错误地序列化为titlesinger,而不是title_elementsinger_element

 {
    "title": "Enter Sandman",
    "singer": "Metallica"
}

重新部署相同的.war文件有时会导致需要的格式。

{
    "title_element": "Enter Sandman",
    "singer_element": "Metallica"
}

pom.xml依赖项

<dependencies>
        <!-- JAX-RS -->
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.1</version>
        </dependency>

        <!-- Jersey -->
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.27</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>2.27</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>2.27</version>
        </dependency>

        <!-- Jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.6</version>
            <exclusions>
                <exclusion>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-core</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-annotations</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.6</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.6</version>
        </dependency>

    </dependencies>

我正在使用apache-tomcat-9.0.11-windows-x64。

当我在Tomcat中重新部署相同的war文件并在localhost:8080 / jsonprop / json / metallica / post上发出发布请求时,如果我请求接收应用程序/,则会得到"title""title_element" json,这是一个问题,因为我需要始终获取“ title_element”。

Link for war file

0 个答案:

没有答案