I am using Jersey to create a web service for a server component. Unfortunately I have a problem with the formatting of my JSON response, because the data is always formatted as string.
I have a simple JAXB annotated POJO with different field types:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class JaxbPojo {
@XmlElement(name = "id_64")
private Long id;
@XmlElement(name = "desc")
private String description;
private Boolean active;
// getters and setters
..
}
and a a simple resource, returning the incoming object:
@Path("/jaxb_pojo")
public class JaxbPojoResource {
@POST
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public JaxbPojo processBidRequest(JaxbPojo pojo) {
return pojo;
}
}
When sending a request to the resource with the following JSON data, the object is filled correctly inside the resource method. But the field types of the returned JSON data are always formatted as string:
me@host:/tmp $ cat simple_jaxb_pojo
{"id_64":99,"desc":"simple JAXB POJO","active":true}
me@host:/tmp $ curl -X POST -H "Content-Type: application/json" --data-binary @simple_jaxb_pojo http://localhost/srvr/rest/jaxb_pojo
{"id_64":"99","desc":"simple JAXB POJO","active":"true"}
All used jar files (jsr311-api-1.1.1.jar, jersey-core-1.6.jar, jersey-json-1.6.jar, jersey-server-1.6.jar) are directly included in the tomcat/lib path. The JAX-RS library is used via Maven pom.xml with scope “provided”:
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
My web.xml looks like this:
<servlet>
<servlet-name>RestServlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.rest.RestResourcesApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
..
<servlet-mapping>
<servlet-name>RestServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Am I missing something or do I maybe have to configure Jackson explicitly in a way that it is using the types of my POJO. I am fairly new to this subject, so I posted every information I have on this. I hope someone can help.
Interesting problem. I’m not sure exactly why this is happening but try adding:
to your Jersey servlet definition in web.xml. I thought Jersey required this to produce JSON but when I removed this init-param I saw the same issue that you’re having.