java 获取响应的 Spring restTemplate 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28855023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:17:23  来源:igfitidea点击:

Spring restTemplate issue in getting response

javajsonspringrestresttemplate

提问by Muhammad Imran Tariq

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

当我使用 rest 客户端软件调用它时,我的 rest 服务器正在生成响应。当我用resttemplate上面提到的代码调用它时,服务器生成响应(打印日志)但resttemplate不执行任何操作(调用后不执行下一行)并打印内部错误

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

这是我通过 restTemplate 调用它的方式

ResponseEntity<Map> response1 = restTemplate.getForEntity(finalUrl.toString(), Map.class);

回答by Sergey Yamshchikov

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

尝试使用ParameterizedTypeReference而不是通配符Map。它应该是这样的。

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

ResponseEntity<Map<String, Object>> response = restTemplate.exchange(finalUrl.toString(), HttpMethod.GET, null, typeRef);

回答by user3193801

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);
}

回答by Abhishek Singh

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

您可以捕获 HttpStatusCodeException ,您可以从中获得 String 的响应。下面的代码对我有用。

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

}