3

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.

Aliyu Fonyuy
  • 515
  • 1
  • 5
  • 13
  • I know a default constructor is required for the reader, but I'm not sure about the writer. You can try and add a default constructor in your model class. – Paul Samsotha Jul 02 '16 at 06:58
  • Thanks @peeskillet , this was the issue. I cant believe I got trapped by that. – Aliyu Fonyuy Jul 03 '16 at 02:05
  • Given the answer was in the comment, how do i mark this as answered? @peeskillet do you want to make the comment an answer instead so i close this off? (Unless there's another way to achieve the same when answer is in comment) – Aliyu Fonyuy Jul 03 '16 at 15:03

1 Answers1

1

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

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720