小编Roh*_*aik的帖子

Spring boot RestClient post 没有请求正文的对象会导致错误的请求

我试图在没有请求正文的情况下创建一个restTemplate.postForObject(),但我收到了错误的请求。

HttpHeaders headers = new HttpHeaders();
      headers.set(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken);
      headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
      HttpEntity<String> entity = new HttpEntity<>(headers);

      CurrentAccount account = client.postForObject(
          "https://api.dropboxapi.com/2/users/get_current_account", entity, CurrentAccount.class);
Run Code Online (Sandbox Code Playgroud)

错误

Caused by: org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: [Error in call to API function "users/get_current_account": Bad HTTP "Content-Type" header: "application/x-www-form-urlencoded".  Expecting one of "application/json", "application/json; charset=utf-8", "text/plain; charset=dropbox-cors-hack".]
Run Code Online (Sandbox Code Playgroud)

添加时

 headers.setContentType(MediaType.APPLICATION_JSON);
Run Code Online (Sandbox Code Playgroud)

我收到一个错误

Caused by: org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: [Error in call to API function "users/get_current_account": request body: could not decode input as JSON]
Run Code Online (Sandbox Code Playgroud)

邮递员请求无需正文即可工作

rest resttemplate spring-boot

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

rest ×1

resttemplate ×1

spring-boot ×1