採用gradle構建工具建立第一個基於jersey的Rest服務
阿新 • • 發佈:2019-01-22
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字串無法解析出實體類