How to handle/create new content-type/MediaType in JAX-RS?

jax-rs, jersey, rest, resteasy

Solution

Yes, you can use `@Produces` and `@Consumes` with custom media types. In order to use the custom media type when marshalling and unmarshalling content you need to create `MessageBodyWriter` and `MessageBodyReader` implementations to handle the media type.

Here is how to implement a custom media type:

Annotate your resource methods with `@Consumes({"application/mycustomtype})` and `@Produces({"application/mycustomtype})` as required.

Implement custom `MessageBodyReader` and `MessageBodyWriter` implementations to support your custom media type.

Annotate your `MessageBodyReader` with `@Provider` and `@Consumes({"application/mycustomtype})`

Annotate your `MessageBodyWriter` with `@Provider` and `@Produces({"application/mycustomtype})`

Problem

I am researching on Jersey and RESTEasy. Media-type negotiation for XML and JSON works fine, and I am able to consume and produce both of them. However, I am being asked to produce and consume a response for a new content-type. For instance, BSON, or a self customized content-type. I googled online but could not find much information in it. Is there anyway, I could still use the @Produces and @Consumes annotation in JAX-RS for the new content-type? Thanks in advance.

Original source

Related problems