Note that there are some explanatory texts on larger screens.

plurals
  1. POJackson provider not deserializing POST data into Pojo (Jersey 2.0)
    primarykey
    data
    text
    <p>I am using Jackson json provider with Jersey 2.0. I have a Web resource like this:</p> <pre><code>@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 { // Authenticate .. generate token .. return Response.ok(token, MediaType.APPLICATION_JSON).build(); } catch (Exception e) { e.printStackTrace(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } } } </code></pre> <p>The UserCredentials Pojo class is as follows:</p> <pre><code>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; } } </code></pre> <p>Here is the relevant snippet from web.xml:</p> <pre><code>&lt;servlet&gt; &lt;servlet-name&gt;jersey-rest-service&lt;/servlet-name&gt; &lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/servlet-class&gt; &lt;init-param&gt; &lt;param-name&gt;com.sun.jersey.api.json.POJOMappingFeature&lt;/param-name&gt; &lt;param-value&gt;true&lt;/param-value&gt; &lt;/init-param&gt; &lt;init-param&gt; &lt;param-name&gt;jersey.config.server.provider.packages&lt;/param-name&gt; &lt;param-value&gt;com.xxxxx.apps.ws.services;com.fasterxml.jackson.jaxrs.json;com.xxxxxx.apps.servlet;com.xxxxxx.apps.ws.filters&lt;/param-value&gt; &lt;/init-param&gt; &lt;init-param&gt; &lt;param-name&gt;com.sun.jersey.spi.container.ContainerRequestFilters&lt;/param-name&gt; &lt;param-value&gt;com.xxxxxx.apps.ws.filters.LoggingFilter&lt;/param-value&gt; &lt;/init-param&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; </code></pre> <p>Here is how the POST entity data looks like:</p> <pre><code>{"username":"xxxxx", "password":"xxxxxx", "ipAddress": "xxx.xxx.xxx.xxx"} </code></pre> <p>Unfortunately, the Jackson provider does not deserialize the above JSON. A null UserCredentials object is injected into the above Web resource POST method. If I use a custom MessageBodyReader, my Reader's readFrom method is invoked and I am able to create the UserCredentials pojo which is then available in the POST method.</p> <p>A couple of questions:</p> <p>1) Do I need to do any special annotation on the Pojo for Jackson to recognize it? Does the Pojo's package need to be added in web.xml?</p> <p>2) Is this property in web.xml relevant anymore: "com.sun.jersey.api.json.POJOMappingFeature" ?</p> <p>3) Do I need to add an ObjectMapper? I think it needs to be done only for custom cases, but please advise.</p> <p>3) Any other gotchas?? Is there a way to debug code in Jackson?</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload