Jackson Provider Does Not Deserialize POST Data in Pojo (Jersey 2.0)

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

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.

+4
source share
3 answers

1) You do not need a special annotation

2) No POJOMappingFeature is no longer like

3) No, you do not need to add ObjectMapper

4) Yes any other gotchas:

Write a class that extends javax.ws.rs.core.Application , and add JacksonFeature to your configured classes (you should have it in your class path, add maven to your configuration):

 package com.example; public class YourApplication extends Application { @Override public final Set<Class<?>> getClasses() { HashSet<Class<?>> set = new HashSet<>(1); set.add(JacksonFeature.class); return set; } } 

Add the following parameter to your web.xml under the servlet configuration for Jersey:

  <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.example.YourApplication</param-value> </init-param> 

That should do it. Unfortunately, this got a little trickier with Jersey 2.0.

+2
source

If you are using the standard Jackson 2.x JAX-RS provider from:

https://github.com/FasterXML/jackson-jaxrs-providers

you do not need to do anything except add to the classpath; it has SPI metadata that should automatically register the provider.

Provider Jackson 1.x did not add this, fearing that it might interfere with other options. But with 2.x, there seems to be nothing to do.

0
source

I understood what happened. I used LoggingFilter to handle the request. He read the object from the stream of the entity. Thus, Jackson had nothing to read and process. Stupid me!

0
source

Source: https://habr.com/ru/post/1490398/


All Articles