users@jersey.java.net

[Jersey] Re: Getting MessageBodyWriter not found for media type=application/json Error

From: Robert DiFalco <robert.difalco_at_gmail.com>
Date: Fri, 20 Dec 2013 13:57:05 -0800

Just a note that you may be happier if you also add the exception
mappers into your jackson feature:

I do it like this:

public class JacksonFeature implements Feature {

    public boolean configure(final FeatureContext context) {
        final String disableMoxy =
CommonProperties.MOXY_JSON_FEATURE_DISABLE + '.'
            + context.getConfiguration().getRuntimeType().name().toLowerCase();
        context.property( disableMoxy, true );

        context.register( JsonParseExceptionMapper.class );
        context.register( JsonMappingExceptionMapper.class );
        context.register( ObjectMapperContextResolver.class );
        context.register( JacksonJsonProvider.class,
MessageBodyReader.class, MessageBodyWriter.class );
        return true;
    }
}

The context resolver just allows me to have a singleton ObjectMapper like this.

@Provider
public class ObjectMapperContextResolver implements
ContextResolver<ObjectMapper> {

    @Override
    public ObjectMapper getContext( Class<?> type ) {
        return JsonUtils.getObjectMapperInstance();
    }
}

On Fri, Dec 20, 2013 at 12:30 AM, Matyas Bene <matyas.bene_at_ferratum.com> wrote:
> Hi Jack,
>
>
> Let me tell you what we’re doing. It may, or may not be the “right
> solution”, but it works for us since Jersey 2.0. You’ll also notice we’re
> using Jackson 2.x
>
>
>
> @Provider
>
> public class JacksonWithJoda extends
> com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider {
>
>
>
> public JacksonWithJoda() {
>
> ObjectMapper mapper = new ObjectMapper().registerModule(new
> JodaModule());
>
>
> //mapper.getSerializationConfig().withSerializationInclusion(Include.NON_EMPTY);
>
> mapper = mapper.setSerializationInclusion(Include.NON_NULL);
>
> setMapper(mapper);
>
> }
>
> }
>
>
>
> public class MyJacksonFeature implements Feature {
>
>
>
> // important – disable MOXY
>
> public boolean configure(final FeatureContext context) {
>
> final String disableMoxy =
> CommonProperties.MOXY_JSON_FEATURE_DISABLE + '.'
>
> +
> context.getConfiguration().getRuntimeType().name().toLowerCase();
>
> context.property(disableMoxy, true);
>
>
>
> // context.register(JacksonJaxbJsonProvider.class,
> MessageBodyReader.class, MessageBodyWriter.class);
>
> // context.register(JacksonJsonProvider.class,
> MessageBodyReader.class, MessageBodyWriter.class);
>
> context.register(JacksonWithJoda.class, MessageBodyReader.class,
> MessageBodyWriter.class);
>
> return true;
>
> }
>
> }
>
>
>
> // AppConfig below must be added as a parameter to Jersey, for example
> inside web.xml
>
> // <init-param>
>
> // <param-name>javax.ws.rs.Application</param-name>
>
> // <param-value>myPackage.AppConfig</param-value>
>
> // </init-param>
>
> public class AppConfig extends ResourceConfig {
>
>
>
> public AppConfig() {
>
> super(MyJacksonFeature.class);
>
> // register(LoggingFilter.class);
>
> }
>
> }
>
>
>
> Regards
>
> M.
>
>
>
> From: Jack Lista [mailto:jackalista_at_gmail.com]
> Sent: Thursday, December 19, 2013 22:59
> To: users_at_jersey.java.net
> Cc: ayaskant swain
> Subject: [Jersey] Re: Getting MessageBodyWriter not found for media
> type=application/json Error
>
>
>
> Hi Marek,
>
> Would you please explain to Ayaskant *how* to register the Jackson feature?
> I know this is (sort of) in the documentation, but I couldn't understand
> from the docs exactly what to do to make that happen. I'm betting he's also
> possibly unclear about that as well (and I'd benefit from seeing how this is
> supposed to be done even though we have muddled our way though it, I am not
> confident enough in our solution to offer it as something that should be
> done, we had a terrible time trying to understand how to clearly put Jackson
> into play properly).
>
> That would help, and thanks!
>
>
>
> On Thu, Dec 19, 2013 at 3:34 AM, Marek Potociar <marek.potociar_at_oracle.com>
> wrote:
>
> A very first few sentences from the linked documentation:
>
>
>
> Jersey JSON support comes as a set of extension modules where each of these
> modules contains an implementation of a Feature that needs to be registered
> into your Configurableinstance (client/server). There are multiple
> frameworks that provide support for JSON processing and/or JSON-to-Java
> binding. The modules listed bellow provide support for JSON representations
> by integrating the individual JSON frameworks into Jersey. ...
>
>
>
> Further documentation section describe details how to enable JSON support
> with MOXy (8.1.2), JSON-P (8.1.3), Jackson (8.1.4) or Jettison (8.1.5). So,
> your problem is that you are not registering any of the features. Please
> select an extension module of your choice and follow the user guide to
> register the feature (as needed).
>
>
>
> HTH,
>
> Marek
>
>
>
>
>
> On 19 Dec 2013, at 08:20, ayaskant swain <ayas_swain_at_yahoo.com> wrote:
>
>
>
> Hi,
>
>
>
> I followed section 8.1.1.2 (JAXB based JSON support) in the User Guide
> document of JAX-RS API at this link -
> https://jersey.java.net/nonav/documentation/latest/user-guide.html#d0e4883.
>
>
>
> I have created a RESTful service using JAX-RS API 2.0 & deployed in my
> Tomcat 6.0.37. I want my service to return a Student object in JSON format
> to the REST client. The mapping of Student Java object (Annotated with
> @XmlRootElement) should automatically be taken care by the underlying JAXB
> API.
>
>
>
> But I am getting the below error on my server while trying to achieve this.
>
>
>
> org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:
> MessageBodyWriter not found for media type=application/json, type=class
> com.ayas.rest.model.jaxb.Student, genericType=class
> com.ayas.rest.model.jaxb.Student.
>
>
>
> I searched on google & found some suggestions to use a JACKSON library which
> will act as a provider for the JSON object. But nothing as such is written
> in the Jersey user guide link which I have posted above.
>
>
>
> There is no issue when I am using MediaType as APPLICATION_XML. It is
> perfectly working fine & returning XML string.
>
>
>
> I am attaching my source code of both Server & client. Please help.
>
>
>
> Thanks
>
> Ayaskant
>
> Bangalore
>
> <Student.java><StudentInfoClient.java><StudentInfoService.java><web.xml>
>
>
>
>