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.
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.
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.
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.
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With