RESTEasy之返回JSON数据格式
环境:
JDK1.6
resteasy-jaxrs-2.3.4.Final
说明:本例参考官网里的examples。如果需要更多信息,请参考官网的例子。
1,建立web项目,这里建站名为resteasy-json的web项目。
2,添加如下web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>javax.ws.rs.Application</param-name> <param-value>org.jboss.resteasy.examples.service.LibraryApplication</param-value> </context-param> <context-param> <param-name>resteasy.servlet.mapping.prefix</param-name> <param-value>/resteasy</param-value> </context-param> <listener> <listener-class> org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap </listener-class> </listener> <servlet> <servlet-name>Resteasy</servlet-name> <servlet-class> org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher </servlet-class> </servlet> <servlet-mapping> <servlet-name>Resteasy</servlet-name> <url-pattern>/resteasy/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> </web-app>
3,添加Library.java
package org.jboss.resteasy.examples.service; import java.io.StringWriter; import java.util.ArrayList; import java.util.HashMap; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.xml.bind.Marshaller; import org.jboss.resteasy.annotations.providers.jaxb.json.BadgerFish; import org.jboss.resteasy.examples.data.Book; import org.jboss.resteasy.examples.data.BookListing; import org.jboss.resteasy.plugins.providers.jaxb.json.BadgerContext; import org.jboss.resteasy.plugins.providers.jaxb.json.JettisonMappedContext; /** * @author <a href="mailto:[email protected]">Bill Burke</a> * @version $Revision: 1 $ */ @Path("library") public class Library { private HashMap<String,Book> books = new HashMap<String,Book>(); public Library() { books.put("596529260", new Book("Leonard Richardson", "596529260", "RESTful Web Services")); books.put("333333333", new Book("Bill Burke", "596529260", "EJB 3.0")); } @GET @Path("books/badger") @Produces("application/json") @BadgerFish public BookListing getBooksBadger() { return getListing(); } @GET @Path("books/mapped") @Produces("application/json") // @Mapped // mapped is the default format public BookListing getBooksMapped() { return getListing(); } @GET @Path("books/badger.html") @Produces("text/html") public String getBooksBadgerText() throws Exception { BookListing listing = getListing(); BadgerContext context = new BadgerContext(BookListing.class); StringWriter writer = new StringWriter(); Marshaller marshaller = context.createMarshaller(); marshaller.marshal(listing, writer); return writer.toString(); } @GET @Path("books/mapped.html") @Produces("text/html") public String getBooksMappedText() throws Exception { BookListing listing = getListing(); JettisonMappedContext context = new JettisonMappedContext(BookListing.class); StringWriter writer = new StringWriter(); Marshaller marshaller = context.createMarshaller(); marshaller.marshal(listing, writer); return writer.toString(); } private BookListing getListing() { ArrayList<Book> list = new ArrayList<Book>(); list.addAll(books.values()); return new BookListing(list); } }
4,添加LibraryApplication.java
package org.jboss.resteasy.examples.service; import java.util.HashSet; import java.util.Set; import javax.ws.rs.core.Application; /** * @author <a href="mailto:[email protected]">Bill Burke</a> * @version $Revision: 1 $ */ public class LibraryApplication extends Application { HashSet<Object> singletons = new HashSet<Object>(); public LibraryApplication() { singletons.add(new Library()); } @Override public Set<Class<?>> getClasses() { HashSet<Class<?>> set = new HashSet<Class<?>>(); return set; } @Override public Set<Object> getSingletons() { return singletons; } }
5,添加Book.java
package org.jboss.resteasy.examples.data; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** * @author <a href="mailto:[email protected]">Bill Burke</a> * @version $Revision: 1 $ */ @XmlRootElement(name = "book") public class Book { private String author; private String ISBN; private String title; public Book() { } public Book(String author, String ISBN, String title) { this.author = author; this.ISBN = ISBN; this.title = title; } @XmlElement public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } @XmlElement public String getISBN() { return ISBN; } public void setISBN(String ISBN) { this.ISBN = ISBN; } @XmlAttribute public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
6,添加BookListing.java
package org.jboss.resteasy.examples.data; import java.util.List; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** * @author <a href="mailto:[email protected]">Bill Burke</a> * @version $Revision: 1 $ */ @XmlRootElement(name = "listing") public class BookListing { private List<Book> books; public BookListing() { } public BookListing(List<Book> books) { this.books = books; } @XmlElement(name = "books") public List<Book> getBooks() { return books; } }
7,添加index.jsp
<html> <body> <h2>Hello World!</h2> <a href="resteasy/library/books/mapped" target="_blank">resteasy/library/books/mapped</a><br/> <a href="resteasy/library/books/badger" target="_blank">resteasy/library/books/badger</a> </body> </html>
8,加入必要的lib:
jaxrs-api-2.3.4.Final.jar jettison-1.3.1.jar resteasy-jaxb-provider-2.3.4.Final.jar resteasy-jaxrs-2.3.4.Final.jar resteasy-jettison-provider-2.3.4.Final.jar scannotation-1.0.3.jar
9,部署项目到tomcat里,启动服务器
9,访问http://localhost:8080/resteasy-json/
10,点击页面resteasy/library/books/mapped
11,如果出现下面的内容,则部署成功。
{"listing":{"books":[{"@title":"EJB 3.0","author":"Bill Burke","ISBN":596529260},{"@title":"RESTful Web Services","author":"Leonard Richardson","ISBN":596529260}]}}