users@jersey.java.net

[Jersey] jersey + jersey-media-moxy = MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json

From: Graham Leggett <minfrin_at_sharp.fm>
Date: Mon, 17 Mar 2014 17:54:36 +0200

Hi all,

I have a jersey endpoint defined as follows, which is correctly returning valid XML without a problem. When I request JSON instead of XML, I get the exception below instead of the json I expect.

javax.servlet.ServletException: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class javax.xml.bind.JAXBElement, genericType=class javax.xml.bind.JAXBElement.

The only reference to this code on google is this, which refers to a bug on the client side, while I am seeing the error on the server side: http://stackoverflow.com/questions/18409956/issues-with-json-processing-using-jaxbelement-under-jersey-2-2-with-moxy

The answer refers to checking that the JAXB objects are annotated correctly, which they seem to be. Can someone see what I missed?

I added the following dependency:

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

The interface looks like this:

    @GET
    @Produces({"application/json", "application/xml" })
    @Path("/browse/{mail}")
    Response retrieveUser(@PathParam("mail") String mail, @HeaderParam("If-Modified-Since") java.util.Date If_Modified_Since);

The code looks like this:

        /** {_at_inheritDoc} */
        public Response retrieveUser(String arg0, Date arg1) {

                ObjectFactory userFactory = new ObjectFactory();
                UserType userType = userFactory.createUserType();
                JAXBElement<UserType> user = userFactory.createUser(userType);

                userType.setSn("Super duper test user");
                
                return Response.ok(user).build();
        }

Does this look familiar to anybody?

Regards,
Graham
--