restTemplate 怎么返回json字符串

时间:2026-02-15 16:22:32

1、项目中有处地方需要通过http post构造restful请求,且需要携带正确的header域

restTemplate 怎么返回json字符串

2、自然而然想到了用Spring自带的rest,对应post它都有对应的封装方法。 

restTemplate 怎么返回json字符串

3、然后在业务类上注入这个bean即可,运行之后,对端服务报body域中的json解析失败。

restTemplate 怎么返回json字符串

4、然后debug这两者之间的区别,发现第一种注入的restemplate是通过SimpleCliactory获取实例的,第二种是通过Buffe。

restTemplate 怎么返回json字符串

5、第一种方式比第二种多了一个fastjson转换器,第一种有八个,第二种是七个

restTemplate 怎么返回json字符串

6、第一种方法中,HttpEntity构造方法中,直接传入pojo对象代替原先的json字符串也是可以的,不用JSONObjec方法转

restTemplate 怎么返回json字符串

7、然而如果项目中用了fastJson,restTemplate内部会用fastjson去转这个字符串作为http body域内容,这时发过去的body域内容就不对了。

restTemplate 怎么返回json字符串

© 2026 途途旅游
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com