Susie
Susie

Reputation: 5138

Jersey REST error, MessageBodyWriter not found for media type=application/json

I get MessageBodyWriter not found error when I invoke web service call.

I have included genson-1.1.jar in my classpath which I believe is suppose to contain the MessageBodyWriter implementation. Yet I get this error.

I am using Jersey 2.13.

I have also tried it without registering the "GensonJsonConverter.class" and still gives the same error.

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {  

    ClientConfig config = new ClientConfig();
    WebTarget target = client.target("http://localhost:8080/WebServices/rest/hello");
    target.path("Lion King");
    target.register(GensonJsonConverter.class);
    String response = target.request().accept(MediaType.APPLICATION_JSON).get(String.class);
    resp.getWriter().print(response);
}

@Path("hello")
public class Hello {        
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Book getThisBook(@PathParam("bookName") String bookName){
        return new Book(bookName, "Msn", new Date());
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>WebServices</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
      <servlet-name>Jersey Rest Service</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.webservices</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet>
      <servlet-name>myServlet</servlet-name>
      <servlet-class>com.webservices.servlet.MyServlet</servlet-class>
  </servlet>

  <servlet-mapping>
      <servlet-name>Jersey Rest Service</servlet-name>
      <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
      <servlet-name>myServlet</servlet-name>
      <url-pattern>*.htm</url-pattern>
  </servlet-mapping>

</web-app>

Part of the stack trace:

SEVERE: MessageBodyWriter not found for media type=application/json, type=class com.webservices.entity.Book, genericType=class com.webservices.entity.Book.
Oct 10, 2014 3:19:49 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [myServlet] in context with path [/WebServices] threw exception
javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error

My lib folder

Upvotes: 0

Views: 5856

Answers (1)

eugen
eugen

Reputation: 5916

Yep with latest Jersey releases the way it discovers "extensions" has changed. Genson will soon be updated to work out of the box with these changes. In the meanwhile to fix it:

1) Add a dependency on metainf-services

OR

2) Register it programmatically.

Have a look at the documentation here there is a an explanation in more details with advices on which solution to choose.

EDIT As you don't build all the container programmitcally it would be easier for you to use the metainf-services solution.

Upvotes: 3

Related Questions