• REST
    August 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
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:
<flight flightId="OK123">
<company>Czech Airlines</company>
<flight flightId="OK124">
<company>Czech Airlines</company>

If you want JSON, the following is what you will GET:
[{"@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();
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");
FlightType fligth124 = new FlightType();
fligth124.setCompany("Czech Airlines");
return myFlights;

Join the discussion

Comments ( 9 )
  • Paul Sandoz Friday, August 31, 2007

    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.


  • Jakub Friday, August 31, 2007

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

  • Alexis MP Tuesday, September 4, 2007
  • Jakub Tuesday, September 4, 2007

    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 :-)

  • Jakub Tuesday, September 4, 2007

    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!

  • aircraft parts Thursday, July 3, 2008

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

  • Jakub Friday, July 4, 2008

    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.

  • Shweta Monday, January 19, 2009

    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.

  • Jakub Monday, January 19, 2009

    @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?

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.