日期:2014-05-16 浏览次数:20616 次
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; } }
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; } }
{ "movie": { "@year":"2011", "director":{ "$":"Michael Bay" }, "name":{ "$":"Transformers: Dark of the Moon" } } }