【问题标题】:RestTemplate return typesRestTemplate 返回类型
【发布时间】:2013-06-11 20:56:40
【问题描述】:

当我将字符串对象类型传递给休息服务调用时,字符串“响应”包含休息 url 的 json 表示

RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject("xxxxx", String.class, new Object[]{});

如果我把上面的代码改成:

 RestTemplate restTemplate = new RestTemplate();
MyObject response = restTemplate.getForObject("xxxxx", MyObject.class, new Object[]{});

响应类型现在是 MyObject。

这里发生了什么,是RestTemplate在指定字符串类型时转换为Json,并在指定对象返回类型时转换为指定对象?

【问题讨论】:

    标签: web-services spring rest


    【解决方案1】:

    是的。基本上,RestTemplate 调用第一个参数中指定的 URL。该调用以 json 的形式返回响应。然后它使用MyObject.classHttpMessageConverter 将json 转换为对象。

    它使用已注册的HttpMessageConverters 进行转换,因此您的应用程序必须在您的应用程序中注册一个MappingJacksonHttpMessageConverter

    【讨论】:

    • 我在我的应用程序中找不到任何对 MappingJacksonHttpMessageConverter 的引用,这是默认转换类型吗?
    • yes.. 默认添加MappingJacksonHttpConverter。阅读 thisspringsource 上的博文
    猜你喜欢
    • 2014-08-21
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 2021-03-11
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多