简体   繁体   English

DELETE HTTP 请求参数和请求体的区别

[英]Difference between DELETE HTTP request parameters and request body

I'm trying to understand the behaviour of an HTTP DELETE request.我试图了解 HTTP DELETE 请求的行为。

On my java web application, I defined the following在我的 java web 应用程序上,我定义了以下内容

@DeleteMapping("url/route")
public ResponseEntity<String> route(@RequestParam int param1, @RequestParam long param2, @RequestParam long param3, @RequestParam String param4) {
    System.out.println(param1 + "-" + param2 + "-" + param3 + "-" + param4 );
    return new ResponseEntity<String>(HttpStatus.OK);
}
    
@DeleteMapping("url/route2")
public ResponseEntity<String> route2(@RequestBody String body) {        
    System.out.println(body);
    return new ResponseEntity<String>(HttpStatus.OK);
}

And on a java client, I do the following在 java 客户端上,我执行以下操作

public final void myFunction(final int param1, final long param2, final long param3, final String param4) {
    restTemplate.delete("http://localhost:8090/url/route?"
            + "param1=" + param1 + "&"
            + "param2="+ param2 + "&"
            + "param3=" + param3 + "&"
            + "param4=" + param4, String.class);
    
    restTemplate.delete("http://localhost:8090/url/route2", "SomeText", String.class);
}

The first request works fine, the log is well displayed.第一个请求工作正常,日志显示良好。 However, on the second case, i get the following error但是,在第二种情况下,我收到以下错误

HttpMessageNotReadableException: Required request body is missing: public org.springframework.http.ResponseEntity<java.lang.String> com.thales.atm.thmi.microservice.CanvasController.deleteGraphicObject2(java.lang.String)] HttpMessageNotReadableException:缺少所需的请求正文:公共 org.springframework.http.ResponseEntity<java.lang.String> com.thales.atm.thmi.microservice.CanvasController.deleteGraphicObject2(java.lang.String)]

I use the module spring-boot-starter-web of spring boot (v2.7.1 on my project).我使用 spring 引导的模块 spring-boot-starter-web (我的项目上的 v2.7.1)。

Can someone explain me what's wrong in my ussage of body parameters for this delete request?有人可以解释我在使用此删除请求的正文参数时有什么问题吗?

Thanks!谢谢!

I use the module spring-boot-starter-web of spring boot (v2.7.1 on my project) .我使用spring 启动的模块 spring-boot-starter-web (我的项目上的 v2.7.1)

According to the most recent version of rest template.根据最新版本的 rest 模板。

void delete(String url, Map<String,?> uriVariables)无效删除(字符串 url,地图<字符串,?> uriVariables)

void delete(String url, Object... uriVariables)无效删除(字符串 url,Object ... uriVariables)

void delete(URI url)无效删除(URI 网址)

No delete method expects a request body to be passed in method parameters.没有删除方法期望在方法参数中传递请求正文。 Uri variables are something different. Uri 变量是不同的。 They are passed and then used to modify the URI accordingly.它们被传递,然后用于相应地修改 URI。 If for example your URI was ./some1/some2/{foo} , you pass a uriVariable like 2 and it makes the URI ./some1/some2/2 .例如,如果你的 URI 是./some1/some2/{foo} ,你传递一个 uriVariable 像 2 并且它使 URI ./some1/some2/2

This is not what you need and you currently have no other choice, than to work around this issue, as it remains at your disposal the general exchange method of restTemplate.这不是您所需要的,并且您目前别无选择,只能解决此问题,因为它仍然可以使用 restTemplate 的一般exchange方法。

 <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> > responseType, Object... uriVariables)

Execute the HTTP method to the given URI template, writing the given request entity > to the request, and returns the response as ResponseEntity.对给定的 URI 模板执行 HTTP 方法,将给定的请求实体 > 写入请求,并将响应作为 ResponseEntity 返回。

This is how it could be used.这就是它可以使用的方式。

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN_VALUE);

HttpEntity<String> entity = new HttpEntity<String>("SomeText", headers);
restTemplate.exchange("http://localhost:8090/url/route2", HttpMethod.DELETE, entity, String.class);

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

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