简体   繁体   English

如何在Java中使用HTTPDELETE作为RequestBody发送列表参数

[英]How to send list parameter with HTTPDELETE as RequestBody in java

Hi i have written a rest service to delete multiple files and i am trying to access the rest service. 嗨,我已经写了一个REST Service来删除多个文件,并且我正在尝试访问REST Service。 My input to rest service will be List values which will be id of files that needs to be deleted. 我输入的休息服务将是列表值,该值将是需要删除的文件的ID。 For that i have written the below code 为此,我写了下面的代码

    List<Long> ids=new ArrayList<Long>();
    ids.add(4l);
    ids.add(5l);

    boolean status = false;
    String jsonResponse = null;
    DefaultHttpClient httpClient = null;
    HttpResponse response = null;
    try {
        httpClient = new DefaultHttpClient();

        StringBuilder url = new StringBuilder("http://localhost:8080/api/files");
        URIBuilder builder = new URIBuilder();
        URI uri = builder.build();

        HttpDelete deleteRequest = new HttpDelete(uri);

        //how to set list as Requestbody and send

        response = httpClient.execute(deleteRequest);
    } 

Here i dont know how to set List as entity in request body and send. 在这里,我不知道如何将列表设置为请求正文中的实体并发送。 Can someone help me on this 有人可以帮我吗

A HTTP DELETE request should delete the resource identified by the URI. HTTP DELETE请求应删除URI标识的资源。 The body is not relevant. 身体无关。 To delete a single resource: 要删除单个资源:

DELETE /api/files/123

If you want to delete more than one file in a single request, model a collection of files as a resource. 如果要在单个请求中删除多个文件,请将一组文件建模为资源。 This could be done by listing more than one ID in the URL: 这可以通过在网址中列出多个ID来完成:

GET /api/files/123,456,789

This could return a JSON array with the details of the three files. 这可能会返回带有三个文件详细信息的JSON数组。

To delete these three files, execute a DELETE request agains the same URL: 要删除这三个文件,请再次执行DELETE请求相同的URL:

DELETE /api/files/123,456,789

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM