Japod's blog
Archives
« November 2009
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
     
       
Today
Click me to subscribe
Search

Links
 

View My Stats
« Jersey will soon be... | Main | Jersey 0.2.1 Is Out! »
Friday Aug 31, 2007
JSON Representation of JAXB Object Sample

The example is available at http://mediacast.sun.com/share/japod/JsonFromJaxb.zip

I have bundled all necessary jar files, so everything you need to run the example
is to unzip the archive, cd to JsonFromJaxb and run


% ant dist

It will produce dist/JsonFromJaxb.war, which you can deploy to your favorite Java web container.
My favorite container is GlassFish, so I would continue with


% $AS_HOME/bin/asadmin deploy dist/JsonFromJaxb.war

The flights resource should be then available at something like
http://localhost:8080/JsonFromJaxb/flights
depending on your web container configuration.

Please see how it works for me at the following screenshot:

If you want (i.e. you set Accept HTTP header to "application/xml")
to get XML representation, you will GET this:


<flights>
<flight flightId="OK123">
<company>Czech Airlines</company>
<number>123</number>
<aircraft>B737</aircraft>
</flight>
<flight flightId="OK124">
<company>Czech Airlines</company>
<number>124</number>
<aircraft>AB115</aircraft>
</flight>
</flights>

If you want JSON, the following is what you will GET:


{"flights":{"flight":
[{"@flightId":"OK123","company":{"$":"Czech Airlines"},"number":{"$":"123"},"aircraft":{"$":"B737"}},
{"@flightId":"OK124","company":{"$":"Czech Airlines"},"number":{"$":"124"},"aircraft":{"$":"AB115"}}]}}

The example contains Netbeans project files,
so you might want to open it in the NB IDE. If you want to see the most important
part of the source code, just look at src/java/com/sun/ws/rest/samples/jsonfromjaxb/resources/FlightList.java.

You can see what is inside bellow. Please note, that you can not only GET the JSON representation,
but also PUT a new one.


@UriTemplate(value = "/flights/")
public class FlightList {

static Flights myFlights;
public FlightList() {
}

@HttpMethod(value = "GET")
@ProduceMime({"application/json", "application/xml"})
public Flights getFlightList() {
return getFlights();
}

@HttpMethod(value = "PUT")
@ConsumeMime({"application/json", "application/xml"})
public Response putFlightListAsJson(Flights flights) {
Response.Builder rBuilder = Response.Builder.noContent();
setFlights(flights);
return rBuilder.build();
}

private synchronized void setFlights(Flights flights) {
myFlights = flights;
}

private synchronized Flights getFlights() {
if (null == myFlights) {
myFlights = (new ObjectFactory()).createFlights();
FlightType fligth123 = new FlightType();
fligth123.setCompany("Czech Airlines");
fligth123.setNumber(123);
fligth123.setFlightId("OK123");
fligth123.setAircraft("B737");
FlightType fligth124 = new FlightType();
fligth124.setCompany("Czech Airlines");
fligth124.setNumber(124);
fligth124.setFlightId("OK124");
fligth124.setAircraft("AB115");
myFlights.getFlight().add(fligth123);
myFlights.getFlight().add(fligth124);
}
return myFlights;
}
}

Posted at 05:44PM Aug 31, 2007 by Jakub Podlesak in REST  |  Comments[9]

Comments:

Nice! Want to commit this to the examples?

If you have no content to return then HTTP method can return void. So the PUT method can be: public void putFlightListAsJson(Flights flights).

Also it is not necessary to use a static variable because the life-cycle of the FlightList is per application.

Paul.

Posted by Paul Sandoz on August 31, 2007 at 06:03 PM CEST #

Thanks for the comments, Paul! And: yes, I can incorporate them and add it as a new example, ok?

Posted by Jakub on August 31, 2007 at 06:13 PM CEST #

Small correction, the URL is not http://localhost:8080/JsonFromJaxb/flights but rather http://localhost:8080/JsonFromJaxb/resources/flights/

Posted by Alexis MP on September 04, 2007 at 10:28 PM CEST #

Thanks for the correction, Alexis! Anyway, if you happen to use the wrong one, you can see a Jersey feature in action, which will automatically redirect you (http status code 307) to the right URL :-)

Posted by Jakub on September 05, 2007 at 08:42 AM CEST #

Alexis, I did not notice the missing resources part in the URL. In such case Jersey won't redirect you of course :o[

Thanks for the correction again!

Posted by Jakub on September 05, 2007 at 08:45 AM CEST #

I have tried to run the code as you said but I am having problem and the output is not displayed

Posted by aircraft parts on July 04, 2008 at 06:28 AM CEST #

You can download the latest Jersey bundle ( jersey-0.9-ea.zip)
at https://jersey.dev.java.net/servlets/ProjectDocumentList
and try it from there. The example is in [examples/JsonFromJaxb] subdirectory.
I have just tested it works fine. If you have further issues, please let me know.

Posted by Jakub on July 04, 2008 at 11:29 AM CEST #

I have created dynamic project RestPOC in eclipse.
copied all flight example source code with all jars.

in main example i have tried url
http://localhost/restPOC/flights.but it giving me error file not display.

I am using tomcat.
This example not require sunjaxws.xml file?
what is that sunweb.xml?

Please let me know your comments.

Posted by Shweta on January 19, 2009 at 11:04 AM CET #

@Shweta: it is hard to say what is wrong, without any specific information in hand. The simplest way to make the json-from-jaxb sample work on tomcat (as a servlet based app) is to: download it from http://download.java.net/maven/2/com/sun/jersey/samples/json-from-jaxb/1.0.1/json-from-jaxb-1.0.1-project.zip , change the packaging in pom.xml to war, and copy src/main/webapp/WEB-INF/web.xml from e.g. helloworld-webapp ( http://download.java.net/maven/2/com/sun/jersey/samples/helloworld-webapp/1.0.1/helloworld-webapp-1.0.1-gf-project.zip ) sample to the json-from-jaxb sample. You will need to update package name in the web.xml to com.sun.jersey.samples.jsonfromjaxb
If the above does not work, could you please try to ask for further help at users@jersey.dev.java.net mailing list?

Posted by Jakub on January 19, 2009 at 06:08 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed