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