简体   繁体   中英

Jersey with Grizzly: Getting MessageBodyWriter not found for media type=application/json

Please, bare some patience with me, I know there's already so many such questions and I have not been able to resolve my issue which seem so basic and am not sure what am missing (Have spent hours trying to figure this out). I have used the following maven archetype to generate the project:

mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 -DarchetypeGroupId=org.glassfish.jersey.archetype

In the pom.xml file, I have uncommented the moxy dependency to support JSON

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

I then added a class, Person as below:

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Person {

  private String firstName;
  private String lastName;

  public Person(String firstName, String lastName) {
      this.firstName = firstName;
      this.lastName = lastName;
  }

  public String getFirstName() {
      return firstName;
  }

  public void setFirstName(String firstName) {
      this.firstName = firstName;
  }

  public String getLastName() {
      return lastName;
  }

  public void setLastName(String lastName) {
      this.lastName = lastName;
  }
}

MyResource.java looks like below:

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

@Path("myresource")
public class MyResource {

  @Path("/person")
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public Person getPerson() {
      return new Person("FistName","LastName");
  }

}

I then start the application and issue following to retrieve Person:

http://localhost:8080/myapp/myresource/person

The setup looks so simple, but this isnt working, with the following exception:

org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo SEVERE: MessageBodyWriter not found for media type=application/json, type=class jersey.grizzly.Person, genericType=class jersey.grizzly.Person.

I have not been able to figure out what is it I am missing (and have gone through many suggested answers to no avail) and could use another set of eyes/perspective.

Given MOXy is a derivative of JAXB, and JAXB requires no-arg (default) constructors for its types, you should have a no-arg constructor in your model classes.

See Also

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM