Spring RestTemplate获取原始JSON字符串 [英] Spring restTemplate get raw json string
本文介绍了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();
推荐答案
You don't even need ResponseEntity
s! Just use getForObject
with a String.class
like:
final RestTemplate restTemplate = new RestTemplate();
final String response = restTemplate.getForObject("https://httpbin.org/ip", String.class);
System.out.println(response);
它将打印类似的内容:
{
"origin": "1.2.3.4"
}
这篇关于Spring RestTemplate获取原始JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文