Spring RestTemplate POST发送空的请求正文

时间:2019-07-09 17:30:40

标签: spring spring-boot resttemplate

我很困惑,为什么restTemplate.postForEntity(endpoint,request,Response.class)发送空的请求主体。

HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    HttpEntity<Request> request = new HttpEntity<Request>(request , headers);

    ResponseEntity<Response> response = restTemplate.postForEntity(endpoint, request, Response.class);

有什么想法吗?

请求是简单的POJO,由类级别的@JsonComponent注释

RestTemplate日志记录写出“请求”正文:o.s.web.client.RestTemplate:编写[请求[name =我的测试名称,type = type1 ...

在http请求拦截器中: 向http://endpoint/api/testEndpoint发送POST请求:[{}]

1 个答案:

答案 0 :(得分:0)

尝试使用它。对我有用!

restTemplate.postForObject(endpoint,null,Void.class);

为请求参数发送空值。