您的位置:首页 > 编程语言 > Java开发

Java Restful Web Services (三)——方法注解

2015-03-03 21:01 274 查看
Rest与HTTP的区别是什么?HTTP是一种应用协议,而Rest是一套规则,在目前来说,我们仅仅使用了HTTP协议中最常见的GET和POST方法,而REST是一种教我们如何使用HTTP协议的一种方式,REST方式就是教会我们使用HTTP协议中所定义的所有方法。以下演示JAX-RS所定义的这些资源方法指示符注解包括@GET, @PUT, @POST, @DELETE等等。

import java.util.Arrays;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import cn.com.icbc.bean.Book;
import cn.com.icbc.bean.Books;
import cn.com.icbc.service.BookService;

@Path("books")
public class BookResource {

//	private static final Logger logger = Logger.getLogger(BookResource.class);

private BookService bookService;

public BookResource() {
bookService = new BookService();
}

@Path("/booktest")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayHello() {
return "Hello Books!";
}

@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Books getBooks() {

final Books books = bookService.getBooks();
System.out.println("Books in BookResource = "+Arrays.toString(books.getBookList().toArray()));

return books;
}

@Path("{bookid:[0-9]*}")
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Book getBookByPath(@PathParam("bookid") int bookid) {

System.out.println(bookid);
final Book book = bookService.getBook(bookid);
System.out.println("BookResourcePath = " + book.toString());
return book;
}

@Path("/book")
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Book getBookByParam(@QueryParam("bookid") int bookid) {

final Book book = bookService.getBook(bookid);
System.out.println("BookResourceQuery = " + book.toString());
return book;
}

@POST
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Book saveBook(final Book book) {
System.out.println("enter saveBook");
return bookService.saveBook(book);
}

@Path("{bookid:[0-9]*}")
@PUT
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON,
MediaType.TEXT_XML })
public Book updateBook(@PathParam("bookid") String bookid, final Book book) {
if (book == null) {
return null;
}
return bookService.updateBook(bookid, book);
}

@Path("{bookid:[0-9]*}")
@DELETE
public String deleteBook(@PathParam("bookid") int bookid) {

if (bookService.deleteBook(bookid)) {
return "Deleted book id = " + bookid;
} else {
return "Deleted book failed id = " + bookid;
}
}

}


测试类

import static org.junit.Assert.assertEquals;

import java.util.Arrays;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import cn.com.icbc.bean.Book;
import cn.com.icbc.bean.Books;

public class BookResourceTest {

private WebTarget target;
public static final String BASE_URI = "http://localhost:8090/NoteMail/rest/";

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
}

@Before
public void setUp() throws Exception {
System.out.println("setUp");
Client c = ClientBuilder.newClient();
target = c.target(BASE_URI);
System.out.println(target.getUri());
}

@After
public void tearDown() throws Exception {
}

@Test
public void testGetBooks() {
Books books = target.path("books").request().get(Books.class);
System.out.println("testGetBooks = " + Arrays.toString(books.getBookList().toArray()));
Assert.assertNotNull(books);
}

@Test
public void testGetBookByPath() {
// fail("Not yet implemented");
final int bookId = 3;
System.out.println("testGetBookByPath = "
+ target.path("books/" + bookId).getUri());
final Book book = target.path("books/" + bookId).request()
.get(Book.class);
System.out.println(book == null ? "book is null" : book.toString());
assertEquals(bookId, book.getBookId());
}

@Test
public void testGetBookByParam() {
final int bookId = 3;
final Book book = target.path("books/book")
.queryParam("bookid", bookId).request().get(Book.class);
System.out.println(book == null ? "book is null" : book.toString());
assertEquals(bookId, book.getBookId());
}

@Test
public void testSaveBook() {

final Book newBook = new Book("Java Restful Web Service使用指南"
+ System.nanoTime(), "人民邮电" + System.nanoTime());
final Entity<Book> bookEntity = Entity.entity(newBook,
MediaType.APPLICATION_JSON_TYPE);
System.out.println(bookEntity);
final Book savedBook = target.path("books")
.request(MediaType.APPLICATION_JSON_TYPE)
.post(bookEntity, Book.class);
System.out.println(savedBook.getBookId());
Assert.assertNotNull(savedBook.getBookId());

}

@Test
public void testUpdateBook() {
final Book newBook = new Book(1, "Java Restful Web Service使用指南"
+ System.nanoTime(), "机械工业Update" + System.nanoTime());

final Entity<Book> bookEntity = Entity.entity(newBook,
MediaType.APPLICATION_JSON_TYPE);
System.out.println(bookEntity);
final Book savedBook = target.path("books/1")
.request(MediaType.APPLICATION_JSON_TYPE)
.put(bookEntity, Book.class);
System.out.println(savedBook);
Assert.assertNotNull(savedBook);
}

@Test
public void testDeleteBook() {
String result = target.path("books/2").request().delete(String.class);
System.out.println(result);
Assert.assertNotNull("Deleted book id = 2", result);

}

}


执行结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐