Search code examples
javarestweb-servicestomcat7media-type

How to resolve MessageBodyWriter not found?


Error: org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteToSEVERE: MessageBodyWriter not found for media type=application/vnd.xyzcompany.v1+json, type=class model.OrderStatus, genericType=class model.OrderStatu

OrderStatus is a model class.

Facing this issue when trying to return the Object of class OrderStatus.

The main reason for this is Media Type, when i tried it using application/json it worked, but need to do it with custom media type like: application/vnd.xyzcompany.v1+json

Extra Info: @Produces and @Consumes are used with the same media type and OrderStatus class is defined in the response of the request.

Wherever I read mostly found that, custom mime/media type need to be registered but don't know how to register and implement.

Can anyone help to resolve this issue.

Thanks in advance :)


Solution

  • Using custom media/mime type and serializing/deserializing the received or sent object according to the new media type.

    It just requires a class with "@Provider" annotation and a "@Produces". The produces annotation will be written as: @Produces({"application/customType.v1+json", "application/json"}).

    With a constructor/method having Object mapper serialization and deserialization.

    NOTE: Keep this class in the package where all API classes are.

    Example Code:

    @Provider
    @Produces({MediaType.APPLICATION_JSON, "application/customType.v1+json"})
    public class JacksonJsonProvider extends JacksonJaxbJsonProvider {
    
        public JacksonJsonProvider() {
    
            ObjectMapper objectMapper = new ObjectMapper()
                .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
                .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                .registerModule(new JodaModule())
                .setDateFormat(new RFC3339DateFormat());
    
            setMapper(objectMapper);
        }
    }