Use the gradle build tool to create the first jersey-based Rest service, gradlejersey
REST is a cross-platform, cross-language architecture style, RESTful Web Service is the implementation of REST style in the Web field, JAX-RS standard is the specification of the Java language for RESTful Web Service, jersey is a reference implementation of JAX-RS and a sub-project of Java EE reference implementation project GlassFish; various abstract objects in the REST architecture style are described as resources, the instantaneous status of a resource is called representation and can be expressed in XML, JSON, and Atom formats. RESTful Web Servcie can also be divided into three layers similar to MVC, namely, resource, business, and data access objects. Resources are located at the frontend for receiving requests and returning responses; standard methods defined in JAX-RS2.0 include DELETE, GET, HEAD, OPTIONS, POST, and PUT.
IDE uses IntelliJ IDEA Community Edition 14.1.2 and the build tool uses gradle. After the gradle project is created, the directory structure is as follows:
Because this project depends on jersey's related packages, make the following configuration and define repositories as the oschina image. Otherwise, mavencentral () cannot be blocked ().
Content of the build. gradle file:
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'}
Entity layer:
@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 layer:
Public class BookDao {Map <Integer, Book> books = new HashMap <Integer, Book> (); private static int nextId = 7; public BookDao () {books. put (1, new Book (1, "Zhang San", "C language programming", "simple publishing house", 29); books. put (2, new Book (2, "Zhang San", "Java programming language", "Information Publishing House", 43); books. put (3, new Book (3, "Li Si", "Emy physics", "Educational Press", 22); books. put (4, new Book (4, "Li Si", "Advanced Mathematics", "Education Press", 43); books. put (5, new Book (5, "Confucius", "Analects of Confucius", "Spring and Autumn press", 53); books. put (6, new Book (6, "Forbes", "", "Unnamed Publishing House", 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 resource layer:
@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); }}
After running, you can view the provided rest api and call method through application. wadl. In addition, REST APIs can be tested using RestClient.
Pay attention to the following points when designing a resource class:
1. The resource class must be a public access type
2. The resource class constructor is also a public access type.
3. The project file and code are saved in the UTF-8 format, otherwise it will easily lead to the garbled problem of subsequent debugging.
4. JSON encoding is used and JackSON dependent package needs to be added.
5. The entity class must provide a non-argument constructor. Otherwise, the entity class cannot be parsed from the corresponding JSON string.