Rehan Rehan - 1 year ago 786
reST (reStructuredText) Question

REST POST works correctly with POSTMAN but exception when using Spring RestTemplate

I am writing a Rest client to post JSON data using Spring RestTemplate.
Using POSTMAN and following JSON data in body get the response correctly-

"Name":"This is test",

However when trying to hit the REST API using Spring RestTemplate as follows

ResponseEntity<String> response =,
HttpMethod.POST, getHeaders(), String.class);

private HttpEntity<?> getHeaders() throws JSONException {
JSONObject request = new JSONObject();
request.put("Email", "");
request.put("Id", "18");
request.put("Name", "This is test");
request.put("InCode", "test");

headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
return new HttpEntity<>(request.toString(), headers);

I get the exception-

11:52:56.808 [main] DEBUG o.s.web.client.RestTemplate - Created POST request for "http://server-test/platform/v4/org"
11:52:56.815 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [text/plain, application/json, application/*+json, */*]
12:03:47.357 [main] DEBUG o.s.web.client.RestTemplate - Writing [{"InCode":"test","Email":"","Id":"18","Name":"This is test"}] using [org.springframework.http.converter.StringHttpMessageConverter@6a1aab78]
11:52:57.574 [main] DEBUG o.s.web.client.RestTemplate - POST request for "http://server-test/platform/v4/org" resulted in 500 (Internal Server Error); invoking error handler
Exception in thread "main" org.springframework.web.client.HttpServerErrorException: 500 Internal Server Error
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(
at org.springframework.web.client.RestTemplate.handleResponse(
at org.springframework.web.client.RestTemplate.doExecute(
at org.springframework.web.client.RestTemplate.execute(

Would be thankful for any help.

Answer Source

The Http Error code 500 you are getting means you are correctly hitting the REST service. The problem you might see the detailed response in POSTMAN not see the complete stack trace using REST client is because the reponse.getbody might have some encoding like Byteinputstream which needs to be converted by your REST client to String. You might use Spring DefaultResponseErrorHandler class for this.

Its done at the client end- Extend the DefaultResponseErrorHandler and create a TestHandler class as follows-

    public class TestErrorHandler extends DefaultResponseErrorHandler {

        public void handleError(ClientHttpResponse response) throws IOException {
        //conversion logic for decoding conversion
        ByteArrayInputStream arrayInputStream = (ByteArrayInputStream) response.getBody();
        Scanner scanner = new Scanner(arrayInputStream);
        String data = "";
        if (scanner.hasNext())
            data =;

Pass the custom ErrorHandler to RestTemplate as

RestTemplate restTemplate = new RestTemplate(); 
restTemplate.setErrorHandler(new TestErrorHandler());

If you get an exception the handle error will be called. There you can convert the bytestream or other encoding to String.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download