I'm facing a problem with JSON format usage in a RESTFul ws, I think due to a personal lack of knowledge on this topic.
Scenario: a client (which is actually an EJB 3.1) uses wink to consume a RESTful webservice, exposed by a standalone application using Apache CXF.
The exchanged resource is the following simple POJO:
The RESTFul webservice builds a Response this way:
Please note that if I don't annotate MyResource class with @XmlRootElement(name = "myresource"), I cannot get a response from CXF (it complains that there's no marshaller associated, or something like that).
The problem is that on client side, wink implementation uses internally Jackson, a format like
isn't accepted, because myresource is not a property of class MyResource. I had to use a Wrapper class which has a single property (of type MyResource) to make the thing working.
Before the server send a response to the client, it use the Rest runtime's marshaling mechanism to marshall the response java object into the requested media type for message body.
So to send the response in the format of json, the Rest runtime will use a JSON converter/provider (use JSON library like Jackson, JSON for example) to marshall into the request json format.
For this work, the JSON converter/provider check if the java object is annotated with JSON annotation. If yes, it will marshall successfully.
So to send the response in the format of xml, the Rest runtime will use a XML converter/provider (uses JAXB) to marshall into the request xml format.
For this work, the XML converter/provider check if the java object is annotated with XML annotation. If yes, it will marshall successfully.
So for your case: If the class is NOT jaxb-annotated class (@ XmlRootElement), then it will not work. You got some kind of message writer exception.
So for your case: If the class is jaxb-annotated class (@ XmlRootElement), then it will work
because there is an JAXBJSON converter that can take a jab annotated class and produce a JSON media type.
>>> All Rest Framework has buitl-in converter/providers out of the box. If needed, you can create a new one new.
Once the client received the text-based response (either son/xml), it will use Rest runtime's un-marshaling mechanism to un-marshall the text-based
response into the java object.
Like on the server, if there is no appropriate converters/providers for un-marshalling, you will an exception like message body exception
So for your client Wink rest runtime, you need to check its marshalling/un-marshalling mechanism.
How: Given you use CXF and Wink Check CXF Rest framework marshalling/un-marshalling mechanism. They may call it data binding or …. It does not matter.
Check Wink Rest framework marshalling/un-marshalling mechanism. They may call it data binding or …. It does not matter.