Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 807
wwj Avatar asked Jul 05 '13 03:07

wwj


1 Answers

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:

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

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

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

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

like image 188
gregwhitaker Avatar answered Sep 29 '22 11:09

gregwhitaker