eagertoLearn
eagertoLearn

Reputation: 10162

when custom MessageBodyWriter and MessageBodyReader needs to written in JAX-RS?

My naive understanding (which could be totally wrong) is that whenever an incoming request stream needs to be translated to POJO, a custom messageBodyReader is necessary and for transmitting a Java POJO across the network as Response, a custom messageBodyWritermust be implemented.

Bu according the jersey documentation here (section 7.2.1), it says that

Please note, that this is only a demonstration of how to write a custom entity provider. Jersey already contains default support for entity providers that can serialize JAXB beans into XML.

I do not understand what this is supposed to mean? Does it mean there will be no need for implementing messageReaders if one uses Jersey Framework?

can someone kindly elaborate?

Thanks

Upvotes: 0

Views: 1217

Answers (2)

George S
George S

Reputation: 640

That specific section of the documentation provides an example for writing a MessageBodyReader that converts an entity into an xml document. The excerpt you refer to is explaining that you don't need to write a MessageBodyReader to handle xml because Jersey provides one by default. They provide the example so you can write a MessageBodyReader to handle conversion of the entity stream into your custom pojo (MyWonky.class).

Upvotes: 0

Zhedar
Zhedar

Reputation: 3510

It means that there's some sort of auto conversion between the formats. As Jersey implements the JAX-RS-standard you can use the annotations provided by the standard.
So for example if you want your method to accept data in XML you could just annotate it with @Consumes(MediaType.APPLICATION_XML) and Jersey will try to automatically convert the message XML into POJOs. I didn't work with XML yet, but I used JAX-RS with JSON and it worked just fine for @Consumes and @Produces.
It's possible that you need to annotate your POJOs with XML annotations like @XmlRootElement to make it work, but in concept the main conversion work is done by the framework and no custom implementations are needed in most cases.

Upvotes: 1

Related Questions