当我使用rest客户端软件调用它时,我的休息服务器正在生成响应。当我用上面提到的resttemplate
代码调用它时,服务器会生成响应(打印日志),但resttemplate
什么都不做(调用后没有下一行执行)并打印内部错误。
这是我服务器中的方法
@ResponseBody
public ResponseEntity<Map<String, Object>> name(){......
...
return new ResponseEntity<Map<String, Object>>(messagebody, HttpStatus.OK);
}
这是我通过restTemplate
调用它的方式ResponseEntity<Map> response1 = restTemplate.getForEntity(finalUrl.toString(), Map.class);
答案 0 :(得分:17)
尝试使用ParameterizedTypeReference代替通配符
ParameterizedTypeReference<Map<String, Object>> typeRef = new ParameterizedTypeReference<Map<String, Object>>() {};
ResponseEntity<Map<String, Object>> response = restTemplate.exchange(finalUrl.toString(), HttpMethod.GET, null, typeRef);
答案 1 :(得分:0)
这是一个适合我的例子
@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);
}
答案 2 :(得分:-1)
您可以捕获HttpStatusCodeException,从中可以获取String的响应。 下面的代码对我有用。
restTemplate.postForObject( url, jsonRequest, ResponseData.class );
catch( HttpStatusCodeException codeException )
{
String payload = codeException.getResponseBodyAsString();
System.out.println( payload );
}