MessageBodyWriter not found vogella tutorial

I am trying to recreate the most excellent Vogella tutorial for creating REST with java, JAX-RS and Jersey.

I am using eclipse Kepler with Java-EE perspective, tomcat 7.0.

I created Todo class, TodoResource class with appropriate annotations and deployed to tomcat 7. I imported jaxrs-ri libs into WEB-INF / lib folder as instructed.

Todo class:

package com.vogella.jersey.jaxb.model;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Todo {
  private String summary;
  private String description;
  public String getSummary() {
    return summary;
  }
  public void setSummary(String summary) {
    this.summary = summary;
  }
  public String getDescription() {
    return description;
  }
  public void setDescription(String description) {
    this.description = description;
  }


} 

      

TodoResource with annotations:

package com.vogella.jersey.jaxb.model;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/todo")
public class TodoResource {
  // This method is called if XMLis request
  @GET
  @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
  public Todo getXML() {
    Todo todo = new Todo();
    todo.setSummary("This is my first todo");
    todo.setDescription("This is my first todo");
    return todo;
  }

  // This can be used to test the integration with the browser
  @GET
  @Produces({ MediaType.TEXT_XML })
  public Todo getHTML() {
    Todo todo = new Todo();
    todo.setSummary("This is my first Todo");
    todo.setDescription("This is my first Todo");
    return todo;
  }

}

      

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" 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>com.vogella.jersey.first</display-name>
  <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.vogella.jersey.jaxb</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

      

I also created a client following the instructions.

Test.java:

package com.vogella.jersey.first.client;

import java.net.URI;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientResponse;
import org.glassfish.jersey.message.internal.MediaTypes;

public class Test {

  public static void main(String[] args) {



    ClientConfig config = new ClientConfig();

    Client client = ClientBuilder.newClient(config);

    WebTarget target = client.target(getBaseURI());


    System.out.println(target.path("rest").path("todo").request()

            .accept(MediaType.APPLICATION_XML ).get(Response.class)

            .toString());

    System.out.println(target.path("rest").path("todo").request()

            .accept(MediaType.APPLICATION_JSON ).get(Response.class)

            .toString());


  }

  private static URI getBaseURI() {

      return UriBuilder.fromUri("http://localhost:8080/com.vogella.jersey.jaxb").build();

  }
}

      

Everything works fine for MediaType.APPLICATION_XML - server returns:

InboundJaxrsResponse{ClientResponse{method=GET, uri=http://localhost:8080/com.vogella.jersey.jaxb/rest/todo, status=200, reason=OK}}

      

However, for MediaType, APPLICATION_JSON is what I really need, I get the error:

InboundJaxrsResponse{ClientResponse{method=GET, uri=http://localhost:8080/com.vogella.jersey.jaxb/rest/todo, status=500, reason=Internal Server Error}}

      

Tomcat clearly shows me the problem - it seems to me that it doesn't know how to return a JSON response -

Nov 02, 2014 11:59:19 AM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE: MessageBodyWriter not found for media type=application/json, type=class com.vogella.jersey.jaxb.model.Todo, genericType=class com.vogella.jersey.jaxb.model.Todo.

      

I understand that the jaxrs-ri 2.13 package includes everything needed, including dependencies, to allow me to do this - and I don't need to add any JSON provider. I did so, I tried to add gson, for example downloaded the moxy jars and tried to add them to the WEB-INF / lib folder and deploy - all to no avail. I don't know if I am completely in weed or if I am missing something simple?

+3


source to share


3 answers


I understand that the jaxrs-ri 2.13 package includes everything needed, including dependencies, to allow me to do this - and I don't need to add any JSON providers.

This is really not true. As stated in Jersey User Manual 8.1. Json

Jersey JSON support comes as a set of add-on modules, where each of these modules contains an implementation of a function to be registered with your custom instance (client / server). There are several frameworks that provide support for JSON processing and / or JSON binding to Java. The modules listed below provide support for JSON representations by integrating individual JSON structures in Jersey. Jersey currently integrates with the following modules to support JSON:

  • MOXy . Support for JSON binding via MOXy is the standard and preferred way to support JSON binding in your Jersey applications from Jersey 2.0. When the MOXy JSON module is on the classpath, Jersey will automatically detect the module and seamlessly enable JSON binding via MOXy in your applications. (See Section 4.3 Auto Discovery Capabilities.)

  • Among the few others

So Jersey Master Boot does not come with these add-on modules. We need to get them separately. At the same time, the easiest way to get the required jersey-media-moxy

one is through Maven.

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>2.13</version>
</dependency>

      

If you are not using Maven (which the tutorial looks at , you are not), you will have to search in dependencies. The artifact jersey-media-moxy

has 16 dependencies, but luckily most of them are in the Jersey distribution. So after filtering out what was already included in the Jersey distribution, these are the remaining jars you will need to find yourself (I just created a custom library for testing)



enter image description here

Adding these dependencies will make the example larger . Tested and working as expected when added.

You now have Eclipse, which I believe comes with a Maven plugin (m2e). So perhaps the easiest way to get these dependencies is to create a new Maven project and add the dependency shown above. Once the project has been created, maven should download all additional dependencies to your local Maven Repo. Just grab them from there for your main project.


Other resources / notes

  • Jersey User Manual
  • I would download the Jersey Example package , which is more up to date than the tutorial you are using.
  • If you don't know Maven, I highly recommend learning at least basic dependency management and letting the build framework capture all the dependencies for you. Also all the examples in the sample package use Maven, so this will help you learn the basics.
+5


source


You write any custom MessageBodyWriter for your marshalling from Java to JSON. If so, then you need to have @Produces annotation with "application / json" in the vendor implementation. For more information, see http://h2labz.blogspot.in/2014/12/marshalling-java-to-json-in-jax-rs.html



0


source


If you are using Jersey 2

Web.xml configuration

    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servletclass>

    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.oracle.restful</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>

      

List of required libraries

  • org.eclipse.persistence.moxy-2.6.3.jar
  • org.eclipse.persistence.core-2.6.3.jar
  • org.eclipse.persistence.asm-2.6.3.jar
  • org.eclipse.persistence.antlr-2.6.3.jar
  • jersey-media-moxy-2.23.1.jar
  • jersey-object-filtering-2.23.1.jar

Run the project, it will work. Also check the JAXB class because it will internally use xml annotation to convert pojo object to JAXB

0


source







All Articles