我正在尝试借助http方法POST / PUT方法来发送数据(添加一本新书),但是我尝试了但无法弄清楚。
这是我的Book.java
类,用于存储书籍值
package bookservice;
public class Book {
private int id;
private String title;
private double price;
public Book() {
}
public Book(int id, String title, double price) {
super();
this.id = id;
this.title = title;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
这是我的Books.java
和addBook()
和removeBook()
方法:
package bookservice;
import java.util.ArrayList;
import java.util.List;
public class Books {
private static ArrayList<Book> books = new ArrayList<>();
static {
books.add(new Book(1, "The Outliers", 500));
books.add(new Book(2, "World Is Flat", 550));
}
public static List<Book> getBooks() {
return books;
}
// method to add books
public void addBook(int id, String name, double price) {
books.add(new Book(id, name, price));
}
// method to remove a book with a given book ID
public Book removeBook(int bookID) {
// search the book in entire ArrayList for the given book ID
for (Book bookToRemove : books) {
if (bookToRemove.getId() == bookID) {
//remove the book
books.remove(bookToRemove);
// if found
return bookToRemove;
}
}
// if not found
return null;
}
}
这是我的BookService.java
处理RestApi
package bookservice;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/books")
public class BookService {
List<Book> books;
public BookService() {
books = Books.getBooks();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Book> getBooks() {
return books;
}
@Path("{id}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Book getBook(@PathParam("id") int id) {
for(Book b : books) {
if ( b.getId() == id)
return b;
}
// book with the given id is not found, so throw 404 error
throw new NotFoundException();
}
}
这是index.html
,(这里我没有添加字段来键入bookID
,也没有添加button
来保存数据,因为我可以在实现完成后再做)
<!DOCTYPE html>
<html>
<head>
<title>Book client</title>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="book.js"></script>
</head>
<h2>Books Client</h2>
Book Id :
<br />
<input type="text" id="id" />
<button onclick="getDetails()">Details</button>
<p />
<div id="title"></div>
<p />
<div id="price"></div>
</html>
这是books.js
,有ajax
个呼叫
/**
*
*/
function getDetails() {
jQuery.ajax({
url : "http://localhost:8080/BookService/rest/books/" + $("#id").val(),
type : "GET",
contentType : "application/json",
dataType : 'json',
success : function(data, textStatus, errorThrown) {
// here is your json.
// process it
$("#title").text(data.title);
$("#price").text(data.price);
},
error : function(jqXHR, textStatus, errorThrown) {
$("#title").text("Sorry! Book not found!");
$("#price").text("");
},
timeout : 120000,
});
};
我的问题是:
1。)如何更新BookService.java
以添加一本书(使用POST http方法)和删除一本书(使用DELETE http方法)。
2。在继续更新客户端之前,如何使用浏览器或Soap ui
测试更新的服务?
我已经实现了功能,但是我对这两个问题感到困惑...