Java SPRING引导:RestTemplate postForObject 400错误请求

Java SPRING引导:RestTemplate postForObject 400错误请求,java,rest,spring-mvc,spring-boot,Java,Rest,Spring Mvc,Spring Boot,在使用RestTemplate发送POST请求时,我一直收到400个错误请求。这是我的密码: MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<String, Object>(); requestBody.add("message_id", "msgid"); requestBody.add("message", "qwerty"); requestBod

在使用RestTemplate发送POST请求时,我一直收到400个错误请求。这是我的密码:

    MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<String, Object>();
    requestBody.add("message_id", "msgid");
    requestBody.add("message", "qwerty");
    requestBody.add("client_id", "111");
    requestBody.add("secret_key", "222");

    MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
    headers.add("Accept", "application/json");
    headers.add("Content-Type", "application/json");

    HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<MultiValueMap<String, String>>(requestBody, headers);

    RestTemplate restTemplate = new RestTemplate();
    String response = restTemplate.postForObject("https://abc.com/api/request", httpEntity, String.class);
    JSONParser parser = new JSONParser();
    try {
        JSONObject jsonObject = (JSONObject) parser.parse(response);
        System.out.println(jsonObject.get("status"));
    } catch (ParseException e) {
        e.printStackTrace();
    }
我试图实现的是转换ff。spring的php代码:

<?php
$arr_post_body = array(
  "message_id" => "qwerty",
  "message" => "Welcome to My life!",
  "client_id" => "111",
  "secret_key" => "222"
);

$query_string = "";
foreach($arr_post_body as $key => $frow)
{
    $query_string .= '&'.$key.'='.$frow;
}

$URL = "https://abc.com/api/request";
$curl_handler = curl_init();
curl_setopt($curl_handler, CURLOPT_URL, $URL);
curl_setopt($curl_handler, CURLOPT_POST, count($arr_post_body));
curl_setopt($curl_handler, CURLOPT_POSTFIELDS, $query_string);
curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($curl_handler);
curl_close($curl_handler);
exit(0);

?>

有什么不对劲吗?请注意,我正在发布一个外部linkAPI,它是HTTPS。

在HTTP头中添加accept和content type后,您是否测试了它,如下所示

MultiValueMap<String, Object> headers = new LinkedMultiValueMap<String, Object>();
headers.add("Accept", "application/json");
headers.add("Content-Type", "application/json");

然后添加请求正文,并使用RestTemplate调用postForObject。

任何时候,如果您希望在使用Spring的REST模板的POST请求后返回数据,最好使用RestTemplate.exchange,而不是RestTemplate.postForObject

因此,您的代码应该如下所示:

HttpHeader headers = new HttpHeaders();
headers.add("Content-Type", MediaType.APPLICATION_JSON.toString());
headers.add("Accept", MediaType.APPLICATION_JSON.toString());

MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<String, String>();
requestBody.add("message_id", "msgid");
requestBody.add("message", "qwerty");
requestBody.add("client_id", "111");
requestBody.add("secret_key", "222");

HttpEntity formEntity = new HttpEntity<MultiValueMap<String, String>>(requestBody, headers);

ResponseEntity<AccessToken> response = 
                restTemplate.exchange("https://abc.com/api/request", HttpMethod.POST, 
                                      formEntity, YourPojoThatMapsToJsonResponse.class);
请务必在此处最后记下YourPojoThatMapsToJsonResponse.class。exchange方法将自动查看JSON响应,并尝试将其转换为已定义的映射到该JSON结构的POJO。换句话说,您应该已经创建了该类。如果您没有,JSONParser.class可能在您的情况下工作,但没有保证


试试这个,让我知道它是怎么回事。

正如Saint和RE350所解释的,这很可能是HTTP头问题。下面是我如何解决它的

1-确定需要使用哪个HTTP头 使用Postman chrome使HTTP调用与您希望使用RestTemplate执行的调用相同。它应该是成功的。打开以检查HTTP请求的标头

2-将这些标题与RestTemplate使用的标题进行比较 将配置为监视localhost:8081,并在代码中用localhost:8081替换restTemplate的目标url。 restemplate.exchangehttp://localhost:8081,HttpMethod.POST,httpEntity,String.class; 运行你的应用程序;您将能够看到TCP/IP监视器中使用了哪些HTTP头

3-配置RestTemplate使用的标题,以匹配邮递员的标题 从中获得灵感