aspose file tools*
The moose likes Web Services and the fly likes JSON Design with Jersey Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "JSON Design with Jersey" Watch "JSON Design with Jersey" New topic
Author

JSON Design with Jersey

James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
I am trying to design a JSON object that would work with Jersey and Jackson.

Am fairly new to JSON / Restful programming, so I am wondering if the following is viable.



Get all information regarding iPhone's Verizon provider:

curl -s -X GET -H'Content-Type: application/json' https://localhost:8080/myservice/iphone/b

would return:



Get list of pricing for iPhone's AT&T provider:

curl -s -X GET -H'Content-Type: application/json' https://localhost:8080/myservice/iphone/a?pricing

Would return:



Is it possible to query the JSON object using the URLs I listed?

Any feedback is highly appreciated...
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1778
    
    7

Seems ok to me. I've done something similar quite recently, albeit with a different technology stack (Jetty, RESTeasy and Jettison).


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
Thanks Jelle,

May I ask you what some of the code would possibly look like?

Am really new to Jersey / JSON...

Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1778
    
    7

Well, you could start with something like:

That would allow you to query all the details related to a specific product in JSON format, using the URI scheme you suggested.
It's not flexible enough to support querying specific providers and pricing information, but you can accomplish that with additional paramaters and parameter types(PathParam, QueryParam, MatrixParam). Getting the basic thing to work would be a good first step.

As for the JSON stuff, RESTEasy allowed me to integrate Jettison as a JAXB provider, so instead of a String I had my method simply return a domain object annotated with JAXB stuff, and the JSON marshalling was actually completely transparent. I think Jersey will allow you to do something similar, which may be worth looking into.
James Dekker
Ranch Hand

Joined: Dec 09, 2006
Posts: 215
Thank you so much Jelle!

This code is definitely a good start... Another question, what if JSON was already a file on a filesystem?

e.g.

/home/jdekker/jsonfiles/product.json

How could my web service access it from the filesystem (before I start doing all of these Restful calls) and have it in memory?
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1778
    
    7

James Dekker wrote:Thank you so much Jelle!

This code is definitely a good start... Another question, what if JSON was already a file on a filesystem?

e.g.

/home/jdekker/jsonfiles/product.json

How could my web service access it from the filesystem (before I start doing all of these Restful calls) and have it in memory?


That depends. If all you're going to do is return a JSON file's full content as the of a web service call, you could simply read the file using the standard java.io API. That seems unlikely though, and what you'll probably want to do is parse the JSON file, which you can do with Jackson, so you can pick and choose the approapriate sections to return as a result.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSON Design with Jersey