• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSON Design with Jersey

 
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems ok to me. I've done something similar quite recently, albeit with a different technology stack (Jetty, RESTeasy and Jettison).
 
James Dekker
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jelle,

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

Am really new to Jersey / JSON...

 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic