Reputation: 123
I'm having some trouble with NPE's while trying to use POJOS as Parameters. I'm deploying the war maven generates to a tomcat 7 with default configurations.
The signature of my method 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 invoke the url, the pojo is null and thus I get th 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've tried with jersey 2.13 and 2.6 And jackson 2.4
Additionally I didnt set up an Application, 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 I might need to Register the JacksonFeature, but I couldn't figure out how to do that.
Upvotes: 3
Views: 1166
Reputation: 123
After further review, the code above works fine. The problem was when calling the class, I was sending the payload with lowercased names. The POJO looks like
private String someString;
public void getSomeString() { ... }
public String setSomeString (String s) {...}
The problem in my payload was me sending "someString" instead of "SomeString". This is very relevant as it was the cause that my object was received as null.
After changing the payload the error changed to this. Jackson with JSON: Unrecognized field, not marked as ignorable
Upvotes: 1