Spring RestTemplate gives “500” error but same URL, credentails works in RestClient and Curl

后端 未结 7 1748
眼角桃花
眼角桃花 2020-12-30 03:06

An Url, Credentials works in RestClient UI as well as with Curl where as i\'m getting \"500\" error when access the same via Spring RestTemplate.

I am using the foll

相关标签:
7条回答
  • 2020-12-30 03:11

    Below works fine

    For Post:

    restTemplate.postForObject(url, parametersMap, Employee.class);

    url is : String - rest api URL parametersMap - MultiValueMap Employee - object which needs to be converted from the JSON response

    0 讨论(0)
  • 2020-12-30 03:18

    Though its too late to answer, below solution will help someone if above solutions did not work. For me, the error was with Request object and its header. Your request object should generate JSON as required by api. It should not have any extra field or additional getter method. In my case, I had additional getter method which was adding unnecessary field to request

    postForEntity = restTemplate.postForEntity(uri,entity,String.class);
    
    0 讨论(0)
  • 2020-12-30 03:19

    I have also faced a situation where server response was "500 Internal server error"

    Though I have received success response from Postman for the same parameter value. So the problem was not in server side.

    The problem was typo in parameter name, mismatch spelling between application parameter and server parameter

    Server parameter        -> requestLabel
    Application parameter   -> requestLable
    

    Hope someone new like me get help from this.

    0 讨论(0)
  • 2020-12-30 03:27

    I would suggest to create your HttpComponentsClientHttpRequestFactory and pass it to your RestTemplate as described below:

    ClientHttpRequestFactory requestFactory = new     
          HttpComponentsClientHttpRequestFactory(HttpClients.createDefault());
    
    RestTemplate restTemplate = new RestTemplate(requestFactory);
    

    By this way, you would avoid server-side issues (like facing error code 500) when testing your application.

    I had the same issue that worked in my local environment and not on the server.

    It is a good practice to pass HttpClients.createDefault() to your HttpComponentsClientHttpRequestFactory while constructing it since by default, this factory uses system properties to create HttpClient for your factory and that may cause lots of pain in real server environment. You may also pass your custom HttpClient.

    0 讨论(0)
  • 2020-12-30 03:30

    You are passing name and password as uri variable:

    public <T> T postForObject(java.lang.String url,
                                         @Nullable
                                         java.lang.Object request,
                                         java.lang.Class<T> responseType,
                                         java.util.Map<java.lang.String,?> uriVariables)
    
                              throws RestClientException
    

    docs.spring.io

    If you had some url like: http://yourhost:8080/dosomethingwithemployee/name/password and you extracted name&password from url itself, then it probably would work.

    String url = "http://yourhost:8080/dosomethingwithemployee/{name}/{password}"
    restTemplate.postForObject(url, request, Employee.class, map);
    

    However, I think you have been trying to send name and password in request body:

    public SomeType getResponse(String login, String password) {
            MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
            headers.add("Content-Type", "application/json");
            Employee employee = new Employee();
            employee.setName(login);
            employee.setPassword(password);
            SomeType responseBody = post("http://locahost:8080/dosomethingwithemployee", employee, headers, SomeType.class);
            return responseBody;
        }
    
        public <T> T post(String url, Object requestObject, MultiValueMap<String, String> headers, Class<T> responseType) {
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
    
            HttpEntity request = new HttpEntity(requestObject, headers);
            T responseObject = restTemplate.postForObject(url, request, responseType);
    
            return responseObject;
        }
    
    0 讨论(0)
  • 2020-12-30 03:32
    RestTemplate header Accept problem 
    --> accept - text/plain, application/json, */*
    
    HttpClient 4.x header Accept
    --> accept - application/json
    

    so i fixed

    HttpHeaders headers = new HttpHeaders();
    headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
    

    http://www.manning-sandbox.com/message.jspa?messageID=119733

    0 讨论(0)
提交回复
热议问题