本文介绍了Spring RestTemplate获取原始JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从Spring Rest模板获取原始json字符串?我已经尝试过以下代码,但它会返回不带引号的json,这会导致其他问题,我该如何按原样获取json.
How can I get the raw json string from spring rest template? I have tried following code but it returns me json without quotes which causes other issues, how can i get the json as is.
ResponseEntity<Object> response = restTemplate.getForEntity(url, Object.class); String json = response.getBody().toString();推荐答案
您甚至不需要ResponseEntity!只需使用 getForObject ,带有String.class,例如:
You don't even need ResponseEntitys! Just use getForObject with a String.class like:
final RestTemplate restTemplate = new RestTemplate(); final String response = restTemplate.getForObject("httpbin/ip", String.class); System.out.println(response);它将打印类似的内容:
{ "origin": "1.2.3.4" }更多推荐
Spring RestTemplate获取原始JSON字符串
发布评论