This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

JSON format problem

 
Bartender
Posts: 1251
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all there,

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.

Any ideas ? What am I missing ?



 
Ranch Hand
Posts: 491
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roughly the WHAT:

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.

GOOD LUCK!
 
Claude Moore
Bartender
Posts: 1251
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. I'll have to investigate further.
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!