简体   繁体   English

REST API ArrayList @删除

[英]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.

相关问题 如何使用REST API使用angularjs删除集合? - How to delete collection with angularjs using REST api? 在 java 中使用 rest API 删除 Bitbucket 分支 - Delete Bitbucket Branch using rest API in java 将ArrayList的内容显示为Java Jersey REST API的正确格式的JSON - Displaying contents of ArrayList into properly formatted JSON for Java Jersey REST API Java REST API:返回ArrayList <T> 作为JSON响应问题 - Java REST API: Returning ArrayList<T> as a JSON Response Issue How to create pojo class of Object of ArrayList from REST API response? - How to create pojo class of Object of ArrayList from REST API response? 使用 REST Api 删除 OTDS 用户 - 无法删除同步对象 - Delete OTDS user using REST Api - Cannot delete synchronized objects 解析其余API的json响应并删除某些jsonObjects-JAVA - Parse json response of rest API and delete certain jsonObjects - JAVA 如何在 Hibernate 中使用 REST API 删除多个记录 - How do I delete multiple records using REST API in Hibernate 使用Rest API通过versionId删除Amazon s3对象 - Delete amazon s3 object by versionId using rest API 通过REST API在Azure中删除用户的经理 - Delete user's manager in Azure through REST api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM