I am using Jackson json provider with Jersey 2.0. I have a web resource:
@Path("/accesstokens") public class AccessTokensService { @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response generate(UserCredentials creds) { System.out.println("In generate method.."); System.out.println(creds); try {
The UserCredentials Pojo class is as follows:
public class UserCredentials { private String username; private String password; private String ipAddress; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getIpAddress() { return ipAddress; } public void setIpAddress(String ipAddress) { this.ipAddress = ipAddress; } }
Here is the corresponding snippet from web.xml:
<servlet> <servlet-name>jersey-rest-service</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.xxxxx.apps.ws.services;com.fasterxml.jackson.jaxrs.json;com.xxxxxx.apps.servlet;com.xxxxxx.apps.ws.filters</param-value> </init-param> <init-param> <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name> <param-value>com.xxxxxx.apps.ws.filters.LoggingFilter</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
Here's what the POST entity data looks like:
{"username":"xxxxx", "password":"xxxxxx", "ipAddress": "xxx.xxx.xxx.xxx"}
Unfortunately, the Jackson provider does not deserialize the above JSON. The null UserCredentials object is injected into the above POST method of the web resource. If I use my own MessageBodyReader, my read method reads Reader, and I can create pojo UserCredentials, which is then available in the POST method.
A few questions:
1) Do I need to do any special Pojo annotation for Jackson to find out? Do I need to add a Pojo package to web.xml?
2) Is this property more relevant in web.xml: "com.sun.jersey.api.json.POJOMappingFeature"?
3) Do I need to add ObjectMapper? I think this should be done only for individual cases, but please tell us.
3) Any other errors? Is there a way to debug code in Jackson?
Thanks.