【发布时间】:2015-08-03 13:24:50
【问题描述】:
我有一项服务,它下载包含 JSON 的 JSONP 文件并对其进行处理。我正在尝试以正确的 Spring 方式进行操作,但我还没有找到任何针对这种特定场景的教程(尽管有很多关于将 json 转换为 jsonp 的内容)。
为了说明问题,假设我点击了以下网址:“https://www.example.com/file.jsonp”,其中包含以下内容:
callback(
{
"field1": "value1",
"field2": "value2"
}
);
为了处理 JSON,我需要删除回调(我目前正在使用子字符串手动执行此操作)。 Spring中是否有任何推荐的方式如何直接从这个JSONP文件中获取POJO?
假设我只有带注释的 pojo 并且只想调用:
RestTemplate restTemplate = new RestTemplate();
Pojo pojo = restTemplate.getForObject(url, Pojo.class);
目前,它给了我以下异常:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.berrycloud.blueberry.monitor.price.model.OnDemandPricePOJO] and content type [application/javascript]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
...
感谢您提供的任何提示。我希望这不是重复的。
【问题讨论】:
-
你的类路径中有
Jackson 1或Jackson 2吗?因为您需要默认 JSONHttpMessageConverter -
我的类路径中有 Jackson 2.4.4。