12

My rest server is generating response when I called it with rest client software. When I call it with resttemplate code mentioned above, then server generates response(print logs) but resttemplate does nothing(no next line executes after call) and prints internal error.

This is the method in my server

@ResponseBody
public ResponseEntity<Map<String, Object>> name(){......
...
return new ResponseEntity<Map<String, Object>>(messagebody, HttpStatus.OK);
}

This is the way I am calling it through restTemplate

ResponseEntity<Map> response1 = restTemplate.getForEntity(finalUrl.toString(), Map.class);
1
  • Do you have any additional details about the internal error? Commented Mar 19, 2015 at 8:56

3 Answers 3

23

Try to use ParameterizedTypeReference instead of wildcarded Map. It should looks like this.

ParameterizedTypeReference<Map<String, Object>> typeRef = new ParameterizedTypeReference<Map<String, Object>>() {};

ResponseEntity<Map<String, Object>> response = restTemplate.exchange(finalUrl.toString(), HttpMethod.GET, null, typeRef);
Sign up to request clarification or add additional context in comments.

Comments

0

this is a example that works for me

@RequestMapping(value = "/getParametros/{instancia}", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<String> getParametros(@PathVariable String instancia)
{
    LOG.debug("REST. Obteniendo parametros del servidor " + instancia);
    Map<String, String> mapa = parametrosService.getProperties(instancia);
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=UTF-8");
    headers.add("X-Fsl-Location", "/");
    headers.add("X-Fsl-Response-Code", "302");
    ObjectMapper mapper = new ObjectMapper();

    String s = "";
    try
    {
        s = mapper.writeValueAsString(mapa);
    } catch (JsonProcessingException e)
    {
        LOG.error("NO SE PUEDE MAPEAR A JSON");
    }

    if (mapa == null)
        return new ResponseEntity<String>(HttpStatus.BAD_REQUEST);

    return new ResponseEntity<String>(s, headers, HttpStatus.OK);
}

Comments

-1

you can Catch the HttpStatusCodeException from which you can get response in String . below code works for me.

restTemplate.postForObject( url, jsonRequest, ResponseData.class );
catch( HttpStatusCodeException codeException )
{
    String payload = codeException.getResponseBodyAsString();
    System.out.println( payload );

}

1 Comment

expecting an exception for a successful request so you can extract a body is a terrible practice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.