How can I send a JSON object from Jersey to the leisure service

I am trying to make a knitwear service because I am using the knitwear example for a maven project. So this is what I got:

my pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>simple-service-webapp</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>simple-service-webapp</name> <build> <finalName>simple-service-webapp</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <inherited>true</inherited> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> <dependencyManagement> <dependencies> <dependency> <groupId>org.glassfish.jersey</groupId> <artifactId>jersey-bom</artifactId> <version>${jersey.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <!-- use the following artifactId if you don't need servlet 2.x compatibility --> <!-- artifactId>jersey-container-servlet</artifactId --> </dependency> <!-- uncomment this to get JSON support <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-moxy</artifactId> </dependency> --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-json-provider</artifactId> <version>2.6.0</version> </dependency> </dependencies> <properties> <jersey.version>2.19</jersey.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> </project> 

my web.xml:

 <?xml version="1.0" encoding="UTF-8"?> <!-- This web.xml file is not required when using Servlet 3.0 container, see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html --> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>Jersey Web Application</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.example</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey Web Application</servlet-name> <url-pattern>/webapi/*</url-pattern> </servlet-mapping> </web-app> 

resource

 package com.example; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; /** * Root resource (exposed at "myresource" path) */ @Path("/myresource") public class MyResource { /** * Method handling HTTP GET requests. The returned object will be sent * to the client as "text/plain" media type. * * @return String that will be returned as a text/plain response. */ @GET @Produces(MediaType.TEXT_PLAIN) public String getIt() { return "Got it!"; } @Path( "complexObject/{name}" ) @GET @Produces( { MediaType.APPLICATION_JSON } ) public ComplexObject complexObject( @PathParam( "name" ) String name ) { return new ComplexObject(name); } } 

The ComplexObject class is just a class with a string. when I hit the URL: http: // localhost: 8080 / simple-service-webapp / webapi / myresource / This work, I get "Got it!"

but when I click: http: // localhost: 8080 / simple-service-webapp / webapi / myresource / complexObject / capo

I get this error on the console:

SEVERE: MessageBodyWriter not found for media type = application / json, type = class com.example.ComplexObject, genericType = class com.example.ComplexObject.

How can i fix this?

+6
source share
1 answer

With this addiction

 <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-json-provider</artifactId> <version>2.6.0</version> </dependency> 

You still need to register a provider. You can register it individually

 <init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value> com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider </param-value> </init-param> 

Or, since the dependency also comes with Jackson ExceptionMappers, you may need to simply scan the entire package (just add it to the package list)

 <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value> com.example, com.fasterxml.jackson.jaxrs.json </param-value> </init-param> 

Another option, in addition to using the above dependency, is to use a Jersey โ€œwrapperโ€ dependency (which handles vendor registration, among a couple of other things like Jackson Filter Filtering . Only used

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

With this dependency registration is required. It is automatically registered through a function with automatic detection

+2
source

All Articles