Reputation: 123
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.
Upvotes: 12
Views: 7241
Reputation: 13410
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})
Upvotes: 19