Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 following code:

MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
map.add("name", user);
map.add("password", password);
restTemplate.postForObject(url, request, Employee.class, map);

Please let me know your suggestions or comments to fix the problem.

like image 341
Kathir Avatar asked Jun 24 '12 16:06

Kathir


People also ask

When API returns 500?

A 500 Internal Server Error is an HTTP status code that indicates that the server encountered an unexpected error while processing the request. Note: Reach out to the API service and check the status page of the API Service and see if their systems are operational.

What is status 500 internal server error?

The HyperText Transfer Protocol (HTTP) 500 Internal Server Error server error response code indicates that the server encountered an unexpected condition that prevented it from fulfilling the request. This error response is a generic "catch-all" response.

How do you use RestTemplate postForObject?

4.1. RestTemplate's postForObject method creates a new resource by posting an object to the given URI template. It returns the result as automatically converted to the type specified in the responseType parameter.


2 Answers

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.

like image 158
Youness Avatar answered Sep 30 '22 10:09

Youness


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

like image 9
webmadeup Avatar answered Sep 28 '22 10:09

webmadeup