简体   繁体   English

RestTemplate.postForObject - 错误:org.springframework.web.client.HttpClientErrorException:400错误请求

[英]RestTemplate.postForObject - Error: org.springframework.web.client.HttpClientErrorException: 400 Bad Request

I'm trying to consume a service in this way: 我试图以这种方式使用服务:

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

public class StatesAPI {
    private RestTemplate restTemplate;
    private String apiEndpoint = "http://service/Geo.svc/getsomethingJson?format=json";

    public static void main(String[] args) {
        StatesAPI s = new StatesAPI();
        s.foo("CA");
    }

    public void foo(String state) {
        String requestBody = "{\"statename\":\"" + state + "\"}";
        String apiResponse = getRestTemplate().postForObject(apiEndpoint,
                requestBody, String.class);
        System.out.println(apiResponse);
    }

    public RestTemplate getRestTemplate() {
        // TODO: Fix the RestTemplate to be a singleton instance.
        restTemplate = (this.restTemplate == null) ? new RestTemplate()
                : restTemplate;
        HttpMessageConverter<?> formHttpMessageConverter = new FormHttpMessageConverter();
        HttpMessageConverter<?> stringHttpMessageConverternew = new StringHttpMessageConverter();
        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(formHttpMessageConverter);
        converters.add(stringHttpMessageConverternew);
        restTemplate.setMessageConverters(converters);
        return restTemplate;
    }

    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }
}

but when I run it I got this error: 但是当我运行它时,我收到了这个错误:

09/10/2013 10:10:32 AM org.springframework.web.client.RestTemplate handleResponseError
ADVERTENCIA: POST request for "[here the link in the code]" resulted in 400 (Bad Request); invoking error handler
Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 400 Bad Request
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:76)
    at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:486)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:443)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:279)
    at StatesAPI.foo(StatesAPI.java:20)
    at StatesAPI.main(StatesAPI.java:15)

Try these modifications to your getRestTemplate: 尝试对getRestTemplate进行这些修改:

public RestTemplate getRestTemplate() {
    // TODO: Fix the RestTemplate to be a singleton instance.
    restTemplate = (this.restTemplate == null) ? new RestTemplate() : restTemplate;

    // Set the request factory. 
    // IMPORTANT: This section I had to add for POST request. Not needed for GET
    restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());

    // Add converters
    // Note I use the Jackson Converter, I removed the http form converter 
    // because it is not needed when posting String, used for multipart forms.
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

    return restTemplate;
}

I think you are missing headers and proper request body. 我认为你缺少标题和正确的请求正文。 Try this, 试试这个,

    public void foo(String state) {
        MultiValueMap<String, Object> headers = new LinkedMultiValueMap<String, Object>();
        headers.add("Accept", "application/json");
        headers.add("Content-Type", "application/json");
        String requestBody = "{\"statename\":\"" + state + "\"}";
        HttpEntity request = new HttpEntity(requestBody, headers);
            String apiResponse = getRestTemplate().postForObject(apiEndpoint,
                    request, String.class);
            System.out.println(apiResponse);
     }

暂无
暂无

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

相关问题 org.springframework.web.client.HttpClientErrorException:RestTemplate 中的 400 错误请求 - org.springframework.web.client.HttpClientErrorException: 400 Bad Request in RestTemplate org.springframework.web.client.HttpClientErrorException:400错误的请求 - org.springframework.web.client.HttpClientErrorException: 400 Bad Request org.springframework.web.client.HttpClientErrorException$BadRequest: 400 错误请求 - org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request org.springframework.web.client.HttpClientErrorException:400使用HttpEntity restTemplate.exchange的错误请求 - org.springframework.web.client.HttpClientErrorException: 400 Bad Request using HttpEntity restTemplate.exchange org.springframework.web.client.HttpClientErrorException: 400 错误的 PUT 请求 - org.springframework.web.client.HttpClientErrorException: 400 Bad Request for PUT 我收到错误 org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request - I am getting the Error org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request 我收到错误消息“ org.springframework.web.client.HttpClientErrorException $ BadRequest:400错误请求” - I'm getting error “org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request” org.springframework.web.client.HttpClientErrorException 400 RestTemplate.postForEntity - org.springframework.web.client.HttpClientErrorException 400 RestTemplate.postForEntity 如何修复错误org.springframework.web.client.HttpClientErrorException: 400 null,当使用RestTemplate访问微服务时 - How to fix the error org.springframework.web.client.HttpClientErrorException: 400 null, when using RestTemplate to access Microservice org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request binance - org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request binance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM