0

I am serializing a POJO into either JSON and XML depending on the Accept header.

@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();
    }

}

I want to have the name of the XML tag changed along with the JSON key name, i have used @XmlElement for this as it seemed to work with both XML and JSON.

Looking at the POJO class I want serialized into XML and JSON, the problem is that @XmlElement doesn't always work when serializing into JSON after redeploying the .war in Tomcat. Half the time it puts the name declared in @XmlElement(name = ""), and the other half it keeps the name of the field.

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 + "]";
    }

}

The result for this in XML is always correct as <singer_element> and <title_element>

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

but for JSON it is sometimes serialized incorrectly as title and singer instead of title_element and singer_element.

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

Redeploying the same .war file sometimes results in the wanted formatting.

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

pom.xml dependencies

<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>

I am using apache-tomcat-9.0.11-windows-x64.

When I redeploy the same war file in Tomcat and make a post request on localhost:8080/jsonprop/json/metallica/post I get either "title" or "title_element" if I request to receive application/json and that's a problem cause I need to always get "title_element".

Link for war file

Rem
  • 46
  • 6
  • It's kinda hard to provide any solutions if we don't know under what conditions does it produce the incorrect output. If we can't reproduce the problem, I'm not sure we will be able to provide you with any answers. – Paul Samsotha Sep 07 '18 at 06:27
  • I have uploaded the war file so you can reproduce the problem. Deploy the war file in Tomcat and make a post request on http://localhost:8080/jsonprop/json/metallica/post. Try it with Accept - application/xml and Accept - application/json On redeploying the same war file a few times you will see a different result for application/json { "title": "Enter Sandman", "singer": "Metallica" } and – Rem Sep 07 '18 at 06:55

0 Answers0