[英]REST API ArrayList @DELETE
public class BookList {
public static List<Book> Listing= new ArrayList<Book>();
Book book = new Book();
public List<Book> getBooks(){
Book book1=new Book("1", "Name 1","Author 1", "Text");
Book book2=new Book("2", "Name 2","Author 2", "Text");
Book book3=new Book("3", "Name 3","Author 3", "Text");
Book book4=new Book("4", "Name 4","Author 4", "Text");
Listing.add(book1);
Listing.add(book2);
Listing.add(book3);
Listing.add(book4);
return Listing;
}
And I have a Main Java Class where @DELETE function is located. 我有一个@DELETE函数位于的Java主类。
@DELETE
@Path("/{bookID}")
@Produces(MediaType.APPLICATION_XML)
public Response deleteBook(@PathParam("bookiD") int id){
String output = (bookList.getBook(id));
return Response.status(200).entity("Hehehe").build();
}
What I want to get is to when I run this @DELETE in Postman is to provide me back with the list of books that are left in the list and exluclude the one that book which ID I put into link: For example localhost:49000/1 - removes book with ID 1 from the list and return back the rest of the books 我想要得到的是在邮递员中运行此@DELETE时要向我提供列表中剩余的书籍列表,并排除我将其链接到ID中的那本书:例如localhost:49000 / 1-从列表中删除ID为1的书,并退回其余书
That is a simple ArrayList
delete issue 那是一个简单的ArrayList
删除问题
Like your getBook
method, add a deleteBook
method and then implement the method that removes book from your list. 像getBook
方法一样,添加deleteBook
方法,然后实现从列表中删除书的方法。 `list.remove(getBook(id-1)); `list.remove(getBook(id-1)); // //
Then in your public Response deleteBookXML(@PathParam("bookID") int id)
method, first deleteBook
, and instead of returning Response with laughing buddha ( entity("Hehehe")
), as Entity, use bookList as entity. 然后,在您的public Response deleteBookXML(@PathParam("bookID") int id)
方法中,首先使用deleteBook
,而不是以笑佛 ( entity("Hehehe")
)作为实体返回Response,而应使用bookList作为实体。
BookList.list.removeif(x -> x.id.equals(id + "");
return Response.status(200).entity(BookList.list).build();
It appeares that your Book object is using Strings as ids? 看来您的Book对象正在使用字符串作为id?
The above code will use your static list of books and remove a book element with the id equal to the id from the URL. 上面的代码将使用您的静态书籍列表,并从URL中删除ID等于ID的book元素。
The code will then return http status 200 with the list of books as entity. 然后,代码将返回http状态200,其中书籍列表为实体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.