1

Hi I'm new to Spring framework.I have written app which post object to RESTAPI using Resttemplate but i am getting this error

23:07:05.856 [main] DEBUG o.s.web.client.RestTemplate - Created POST request for
  "http://dummyurl.net/Index"    
23:07:05.887 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept
  header to [application/json, application/*+json]
23:07:05.887 [main] DEBUG o.s.web.client.RestTemplate - Writing
  [org.hrishi.ConsumeReStApi.Info@727803de] as "application/json" using
  [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@704921a5]
23:07:05.981 [main] WARN  o.s.web.client.RestTemplate - POST request for "dummyurl.net"
 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.handleResponseError(RestTemplate.java:598)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:556)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:512)
    at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:363)
    at org.hrishi.ConsumeReStApi.App.main(App.java:35)

and this my code

        String url=    "http://dummyurl.net/Index";    
        RestTemplate restTemplate = new RestTemplate();
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
        headers.add("Content-Type", "application/json");
        Info info1=new Info();
        info1.Id="1711";
        info1.Name="Hrishi";
        HttpEntity<Info> HReq=new HttpEntity<Info>(info1,headers);
        ResponseEntity<Info> info = restTemplate.postForEntity(url, HReq, Info.class);
        System.out.print("id : "+info.getBody());

and this is request object

public class Info {

    public String Id;
    public String Name;

    public String getName() {
        return Name;
    }

    public String getId() {
        return Id;
    }

}
hynekcer
  • 14,942
  • 6
  • 61
  • 99
Hrishikesh
  • 23
  • 1
  • 2
  • 5
  • Can you post the server side code(only api) because as per log it throws 500,so may be it's rest api issue? – dReAmEr Oct 30 '14 at 18:17
  • Post the `@Controller` and `@RequestMapping` code. – Manuel Jordan Oct 30 '14 at 18:19
  • Is a **bad** practice have the instance variable how `public`, they must be `private`, that's why you are working with getter/setters. – Manuel Jordan Oct 30 '14 at 18:21
  • instance variable names should start with lowercase, not with uppercase. – Manuel Jordan Oct 30 '14 at 18:21
  • Since you are getting HTTP Status Code 500 then it means it is a server error, not client one. – Rafal G. Oct 30 '14 at 18:51
  • I got it.I was getting this 500 error because variables are declared public in info class. it must be private and it works..@ManuelJordan You'r right.variable must be private.thnxs – Hrishikesh Oct 31 '14 at 17:52
  • Possible duplicate of [Rest Template custom exception handling](http://stackoverflow.com/questions/21429899/rest-template-custom-exception-handling) – Qy Zuo Sep 26 '16 at 08:55

2 Answers2

0

You can try the below code:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Info {

@JsonProperty("Id")
    public String Id;

 @JsonProperty("Id")
    public String Name;

public String getName() {
    return Name;
}

public String getId() {
    return Id;
}
Unheilig
  • 16,196
  • 193
  • 68
  • 98
Naag
  • 11
0

Seems like you are not setting header properly Content-Type' with 'application/json'

      HttpHeaders headers = new HttpHeaders();
      headers.setContentType(MediaType.APPLICATION_JSON);

      HttpEntity<String> entity = new HttpEntity<String>(postBodyJson ,headers);
      restTemplate.put(uRL, entity);
vaquar khan
  • 10,864
  • 5
  • 72
  • 96