Jersey Restful Web Service - MessageBodyProviderNotFoundException

I am new to Java Web Services and I am struggling with a major issue.

After finding a bunch of obsolete examples, I managed to get something that works with XML, but the same code does not work when I ask for JSON to return.

Initially, I thought it was a missing JSON formatter, but JAXB should take care of converting from POJO to JSON, so I don't think the problem is.

Error occurring in Tomcat:

javax.servlet.ServletException: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class resttest.model.Todo, genericType=class resttest.model.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" 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_2_5.xsd"
  id="WebApp_ID" version="2.5">
  <display-name>testtest</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>resttest.jaxb;resttest.model</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app> 

Todo.java

package resttest.model;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Todo {

    public 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.Java

package resttest.jaxb;

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

import resttest.model.Todo;

@Path("/todo")
public class TodoResource {

  @GET
  @Produces("application/json")
  public Todo getTodo() {
    Todo todo = new Todo();
    todo.setSummary("This is my first todo");
    todo.setDescription("This is my first todo");
    return todo;
  }

} 

Any ideas why JSON is not returning and the error was reset?

+4
source share
3 answers

, . TodoResource , Application , MOXyJsonContextResolver, ContextResolver. , Json- Json. , , . MOXy Jersey ( 2.5.1) json-, . , , MOXy jar, maven pom.xml(jersey-media-moxy-2.5.1.jar).

: web.xml. , 2.5.1.

@javax.ws.rs.ApplicationPath("webresources")
public class ApplicationConfig extends Application {

public ApplicationConfig() {
    this.initMethods();
}

@Override
public Set<Class<?>> getClasses() {
    Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
    addRestResourceClasses(resources);
    return resources;
}

private void initMethods() {
    try {
        ...some classes you might need instantiated, etc, for your resource class
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void addRestResourceClasses(Set<Class<?>> resources) {
    resources.add(MOXyJsonContextResolver.class);
}

}

MOXyJsonContextResolver.class, Json:

public class MOXyJsonContextResolver implements ContextResolver<MoxyJsonConfig>  {
private final MoxyJsonConfig config;

public MOXyJsonContextResolver() {

    config = new MoxyJsonConfig()
            .setAttributePrefix("")
            .setValueWrapper("value")
            .property(JAXBContextProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
}

@Override
public MoxyJsonConfig getContext(Class<?> objectType) {
    return config;
}
}
0

: @XmlAccessorType(XmlAccessType.FIELD)

: @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Todo { ...

0

You mentioned @XmlRootElementat the class level in the todo class. @XmlRootElementit is only required if you want to receive an answer in a format xml, and also provide @Pathat the method level in a class TodoResource, this is good practice. mention @Produces(MediaType.APPLICATION_JSON)at the method level. Hope this works for you!

0
source

All Articles