Error parsing JSON parameter in java REST

I recently updated my REST API to use jersey 2.x, and now I can’t get the JSON body parameters the way I'm used to, the methods just don't get called anymore. I assume that I am missing the dependency to parse JSON into a java object, but I'm not too sure what I need to add, any help is appreciated.

pom.xml

<dependencies> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>2.19</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-server</artifactId> <version>2.19</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.19</version> </dependency> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-multipart</artifactId> <version>2.22</version> </dependency> </dependencies> 

REST Method

 @POST @Path("/users/{userId}/friends") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Response followUser(@PathParam("userId") Integer myUserId, FollowUserBean user) {} 

FollowUserBean.java

 import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class FollowUserBean { public Integer friendId; public FollowUserBean() {} } 
0
java json rest maven
Aug 10 '16 at 5:45
source share
2 answers

Do you need a JSON provider

At the time of writing, Jersey 2.x is merging with the following modules to support JSON:

Using Jackson

See the steps below to use Jackson as a JSON provider for Jersey 2.x:

Adding Jackson Module Dependencies

To use Jackson 2.x as your JSON provider, you need to add jersey-media-json-jackson to your pom.xml file:

 <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.25.1</version> </dependency> 

To use Jackson 1.x, it will look like this:

 <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson1</artifactId> <version>2.25.1</version> </dependency> 

Jackson module registration

In addition to adding the dependency mentioned above, you need to register JacksonFeature (or Jackson1Feature for Jackson 1.x) in the Application / ResourceConfig subclass:

 @ApplicationPath("/api") public class MyApplication extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> classes = new HashSet<Class<?>>(); classes.add(JacksonFeature.class); return classes; } } 
 @ApplicationPath("/api") public class MyApplication extends ResourceConfig { public MyApplication() { register(JacksonFeature.class); } } 

If you do not have Application / ResourceConfig , you can register JacksonFeature in the web.xml deployment descriptor. Specified resource names, provider names, and fully-functional class names can be provided in a comma-separated value jersey.config.server.provider.classnames initialization parameter.

 <init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value>org.glassfish.jersey.jackson.JacksonFeature</param-value> </init-param> 

MessageBodyWriter provided by Jackson, JacksonJsonProvider .




For more information, check out the Jersey documentation for general media type support.

+3
Aug 10 '16 at 7:14
source share
β€” -

Perhaps you are missing the support for the JSON vendor support module Jackson (2.x):

 <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.19</version> <scope>compile</scope> </dependency> 

It is recommended that you use the same version version for all libraries.

0
Aug 10 '16 at 7:08
source share



All Articles