Jersey 2.x using Pojo as parameter for POST

I am having problems with NPE when trying to use POJOS as parameters. I am deploying war maven to generate tomcat 7 with default settings.

My method signature looks like this.

@POST
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
@Path("/do/")
public OtherPojo doSomething(MyPOJO pojo) {

      

The pojo is annotated with @XmlRootElement However, when I call the url, the pojo is null and thus I get NPE. My pom.xml

<dependency>
  <groupId>org.glassfish.jersey.ext</groupId>
  <artifactId>jersey-metainf-services</artifactId>
  <version>${jersey.version}</version>
</dependency>
<dependency>
  <groupId>org.glassfish.jersey.containers</groupId>
  <artifactId>jersey-container-servlet</artifactId>
</dependency>
<dependency>
  <groupId>org.glassfish.jersey.media</groupId>
  <artifactId>jersey-media-json-jackson</artifactId>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.jaxrs</groupId>
  <artifactId>jackson-jaxrs-json-provider</artifactId>
  <version>${jackson.version}</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>${jackson.version}</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>${jackson.version}</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>${jackson.version}</version>
</dependency>

      

I tried with jersey 2.13 and 2.6 and Jackson 2.4

Also, I haven't created an app, my classes just have the annotation

@Path("/somePath/")
public class SomeClass {

      

And this is configured in my web.xml

    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.beer.routes</param-value>
    </init-param>

      

I read that I might need to register the JacksonFeature, but I couldn't figure out how to do this.

+3


source to share


1 answer


Upon further review, the code above works fine. The problem was calling the class, I was posting the payload with lowercase names. POJO looks like

private String someString;

public void getSomeString() { ... }
public String setSomeString (String s) {...}

      



The problem with my payload was that I was sending "someString" instead of "SomeString". This is very relevant as this was the reason my object was received as null.

After changing the payload, the error changed to this. Jackson with JSON: unrecognized field not marked as ignorant

+1


source







All Articles