RESTEasy Could not find MessageBodyWriter for response object of type: <Entity Class> of media type: application/json

2.7k Views Asked by At

I'm trying to implement a rest endpoint using rest easy. This is a GET endpoint,

    @Controller
@Path("/api")
public class TestController {

    private static final Log LOG = LogFactory.getLog(TestController .class);


    @GET
    @Path("/test")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getTest() {

        LOG.info(" inside test");

        Response r = null;
        try {

         Test test = new Test();
         test.setId(1L);
         test.setName("test");
         test.setAge("20");

         r = Response.ok(test).build();

        } catch (Exception e){
            LOG.error(e);
        }

        return r;
    }
}

Below is the entity class which I'm trying to return

@XmlRootElement
public class Test {

    @XmlElement(name = "id")
    private Long id;

    @XmlElement(name = "name")
    private String name;

    @XmlElement(name = "age")
    private String age;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

Getting below error when the endpoint is called from a rest client.

Could not find MessageBodyWriter for response object of type: com.package.Test of media type: application/json

These are some dependencies I have added which I believe would be useful for this.

httpclient-4.0.3.jar
httpcore-4.0.1.jar
jackson-core-asl-1.6.3.jar
jackson-jaxrs-1.9.13.jar
jackson-jaxrs-json-provider-2.2.1.jar
jackson-mapper-asl-1.6.3.jar
jackson-xc-1.6.3.jar
jaxrs-api-3.0.12.Final.jar
jboss-logging-3.3.1.Final.jar
jcip-annotations-1.0.jar
resteasy-jaxb-provider-3.1.0.Final.jar
resteasy-jettison-provider-2.3.1.GA.jar
resteasy-spring-2.2.1.GA.jar
scannotation-1.0.3.jar

Does anyone has an idea why this kind of error coming. endpoint is able return a plain string as a response.

1

There are 1 best solutions below

0
On

jackson-jaxrs-json-provider contains the MessageBodyReader/Writer to handle JSON/POJO conversion. But you still need to register its JacksonJaxbJsonProvider with your application.

If you are using RESTEasy, you can also just add the resteasy-jackson-provider dependency to your project and it will automatically register the JacksonJaxbJsonProvider so you don't need to explicitly do it. The dependency also adds a few other useful items. So your best bet is to just add the dependency.