14

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-

{
    "InCode":"test",
    "Name":"This is  test",
    "Email":"test@gmail.com",
    "Id":18,
}

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

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

private HttpEntity<?> getHeaders() throws JSONException {
JSONObject request = new JSONObject();
        request.put("Email", "test@gmail.com");
        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":"test@gmail.com","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(DefaultResponseErrorHandler.java:94)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:475)

Would be thankful for any help.

Rehan
  • 929
  • 1
  • 12
  • 29
  • Looks like your are performing a request to the base URL of your application. I think that's not what you want. – cassiomolin May 20 '16 at 07:30
  • You can go through this [Link](http://restcookbook.com/HTTP%20Methods/400-vs-500/) which clearly tells when which types of error will be returned by REST. Can you give full error trace to track it down further? – darshgohel May 20 '16 at 08:17
  • The URL is correct. And this is the complete StackTrace. It only does not have the line number where it was occurring during call, – Rehan May 20 '16 at 09:24
  • Did you ever figure this out? – JS noob Oct 12 '20 at 22:16

3 Answers3

6

You are setting the 'Accept' header, which defines which content type you will accept as a response.

Instead you must set the header 'Content-Type' with 'application/json'.

Edit:

In your java code id is a string, in the postman its a number. May be this makes the server fail?

  • 1
    I did add content type but getting the same exceptiom. The service accepted both JSON and XML data. It is working with XML. Also the POSTMAN returns HTTP code 500 in some scenarios but the description is good like the data already exists at server side. But RestTemplate only gives the stacktrace as above. – Rehan May 22 '16 at 18:41
  • 1
    Thank you Stefan. This solved a icky bug that was faced. I started off posting requests to a system via WebResource (Jersey) as part of a POC. All looks fine, once I implemented the code in a project via Spring Rest Template, the other system mentioned that they are receiving an empty JSON. Long story short and trouble shooting procedures, applying headers.setContentType(MediaType.APPLICATION_JSON); resolved the issue. – Rami Del Toro Mar 08 '17 at 14:10
1

Try this way

 try {
        return restTemplate.exchange(url, httpMethod, httpEntity, String.class);
     } catch(HttpStatusCodeException e) {
        return ResponseEntity.status(e.getRawStatusCode()).headers(e.getResponseHeaders())
                .body(e.getResponseBodyAsString());
     }
Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
1

I was facing the same problem. After printing the request and URL, I found that I was using a wrong endpoint. Can you please try to print the URL in the logs and check if that is correct?

Ifta
  • 1,536
  • 18
  • 25