REST是一种跨平台、跨语言的架构风格,RESTful Web Service是REST风格在Web领域的实现,JAX-RS标准是Java语言对RESTful Web Service的规范,Jersey是JAX-RS的参考实现,也是Java EE参考实现项目GlassFish的子项目;REST架构风格中各种抽象对象被描述为资源,资源的瞬时状态称为representation且可用XML/JSON/Atom等格式表示。RESTful Web Servcie也可以分解为类似MVC的三层结构,即资源、业务和数据访问对象,资源位于最前端用于接收请求和返回响应;JAX-RS2.0中定义的标准方法包括DELETE、GET、HEAD、OPTIONS、POST和PUT。
IDE采用IntelliJ IDEA Community Edition 14.1.2,构建工具采用gradle,创建gradle项目后,目录结构如下
![]()
由于本项目需要依赖jersey的相关包,因此作如下的配置,同时定义repositories为oschina镜像,否则无法拦截到mavencentral()。
build.gradle文件内容:
apply plugin: 'java'
apply plugin:'war'
version = '1.0'
repositories {
maven {
url 'http://maven.oschina.net/content/groups/public/'
}
}
dependencies {
compile group:'org.glassfish.jersey.containers',name:'jersey-container-grizzly2-http',version:'2.17'
compile group:'org.glassfish.jersey.media',name:'jersey-media-json-jackson',version:'2.17'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
实体层:
@XmlRootElement(name="book")
public class Book {
Integer id;
String author;
String bookName;
String publisher;
int price;
public Book(Integer id, String author, String bookName, String publisher, int price) {
this.id = id;
this.author = author;
this.bookName = bookName;
this.publisher = publisher;
this.price = price;
}
public void setId(Integer id){
this.id=id;
}
@XmlAttribute
public Integer getId(){
return id;
}
@XmlAttribute
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@XmlAttribute
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@XmlAttribute
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
@XmlAttribute
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
}
DAO层:
public class BookDao {
Map<Integer,Book> books=new HashMap<Integer,Book>();
private static int nextId=7;
public BookDao(){
books.put(1,new Book(1,"张三","C语言程序设计","简单出版社",29));
books.put(2, new Book(2, "张三", "Java语言程序设计", "信息出版社", 43));
books.put(3, new Book(3, "李四", "大学物理", "教育出版社", 22));
books.put(4, new Book(4, "李四", "高等数学", "教育出版社", 43));
books.put(5, new Book(5, "孔子", "论语", "春秋出版社", 53));
books.put(6, new Book(6, "福布斯", "大学英语", "未名出版社", 22));
}
public Book get(Integer id){
return books.get(id);
}
public boolean update(Book bk){
if(bk.getId()==null)
{
bk.setId(nextId++);
}
books.put(bk.getId(),bk);
return true;
}
public boolean delete(Integer id){
if(books.remove(id)==null)
return false;
else
return true;
}
public Map<Integer,Book> getAll(){
return books;
}
}
Web Service资源层:
@Path("books")
public class Books {
private final BookDao bookDao;
public Books() {
bookDao = new BookDao();
}
@GET
@Produces({MediaType.APPLICATION_JSON})
public Map<Integer,Book> getAllBooks(){
return bookDao.getAll();
}
@Path("book")
@GET
@Produces({MediaType.APPLICATION_JSON})
public Book getBook(@QueryParam("id")final int BookId){
return bookDao.get(BookId);
}
@POST
@Produces({MediaType.APPLICATION_JSON})
public Boolean insertBook(final Book bk)
{
return bookDao.update(bk);
}
@PUT
@Produces({MediaType.APPLICATION_JSON})
public Boolean putBook(final Book bk){
return bookDao.update(bk);
}
@DELETE
@Produces({MediaType.APPLICATION_JSON})
public Boolean delBook(@QueryParam(("id"))final Integer bookId){
return bookDao.delete(bookId);
}
}
运行后,可以通过application.wadl查看提供的REST API及调用方法。另外,REST API可以采用RestClient测试。
在设计资源类时必须注意以下几点:
1、资源类必须是public访问类型
2、资源类的构造函数也是public访问类型
3、项目文件及代码采用UTF-8格式保存,否则容易导致后面调试的乱码问题
4、用到JSON编码,需要加入JackSON依赖包
5、实体类要提供无参构造函数,否则从相应的JSON字符串无法解析出实体类