JSON example with RESTEasy + JAXB + Jettison

RESTEasy uses Jettison JSON library to map JAXB annotation object to and from JSON. In this tutorial, we show you how to convert an JAXB annotated object into JSON format and return it back to client.

Jackson as JSON provider

1. RESTEasy + JAXB + Jettison

To use JSON in RESTEasy, you need following dependencies.

File : pom.xml

  <repositories>
    <repository>
        <id>JBoss repository</id>
        <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
    </repository>
  </repositories>

  <dependencies>

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxrs</artifactId>
        <version>2.2.1.GA</version>
    </dependency>

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxb-provider</artifactId>
        <version>2.2.0.GA</version>
    </dependency>

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jettison-provider</artifactId>
        <version>2.2.0.GA</version>
    </dependency>   

  </dependencies>

2. JAXB XML Provider

Create an object, annotate with JAXB. Why using XML provider? No worry, later you will use @BadgerFish to convert it into JSON format.

package com.mkyong.rest;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "movie")
public class Movie {

    String name;
    String director;
    int year;

    @XmlElement
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlElement
    public String getDirector() {
        return director;
    }

    public void setDirector(String director) {
        this.director = director;
    }

    @XmlAttribute
    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

}

3. JAX-RS

To return a JSON file format, annotate the service method with @BadgerFishand @Produces("application/json").

RESTEasy will handle the JSON conversion automatically.

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.jboss.resteasy.annotations.providers.jaxb.json.BadgerFish;

@Path("/json/movie")
public class JSONService {

    @BadgerFish
    @GET
    @Path("/get")
    @Produces("application/json")
    public Movie getMovieInJSON() {

        Movie movie = new Movie();
        movie.setName("Transformers: Dark of the Moon");
        movie.setDirector("Michael Bay");
        movie.setYear(2011);

        return movie; 

    }

}

4. Demo

When URI pattern “/json/movie/get” is requested, following JSON file will be returned.

{
    "movie":
    {
        "@year":"2011",
        "director":{
            "$":"Michael Bay"
        },
        "name":{
            "$":"Transformers: Dark of the Moon"
        }
    }
}

result

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值