我们在服务器端有一个带有Oracle11g和Apex的rest web服务。在客户端,我们正在为android开发,并使用Spring 1.0.1和Jackson 2.2.3库来管理其余Web服务的请求,并将json数据转换回pojo。
当Web服务是一个"查询"时,它工作得非常好。结果集Json数据转换为波荷人的阵列没有问题。但是,当我们尝试对oracle过程执行同样的操作时,它会以异常的方式失败。
程序返回的Json数据如下:
{"item":"{rn "user" : "{john}",rn "profile" : "nothing"rn}"}
我尝试了一个在线Json验证器,Json数据似乎是有效的。在标题中,您还可以看到类型是"application/json"。
pojo对象如下:
public class User {
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
private String user;
public String getProfile() {
return profile;
}
public void setProfile(String profile) {
this.profile = profile;
}
private String profile;
}
调用webservice并尝试将json转换为pojo的代码如下(复制自spring示例):
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
User users = restTemplate.getForObject(url, User.class);
最后,当它尝试执行"getForObject"时的异常:
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [com.xxx.xxx] and content type [text/plain;charset=iso-8859-1]
我试着对Gson图书馆做同样的事情,而不是杰克逊图书馆,同样的例外是trown。现在我被屏蔽了几天。。。
有什么想法吗?我做错了什么?
提前感谢,
问题出在您返回的JSON和您声明的类上。您的JSON结构是{"item":"{rn "user" : "{john}",rn "profile" : "nothing"rn}"}
,它不映射到User类。映射到用户类的Json结构是{rn "user" : "{john}",rn "profile" : "nothing"rn}
因此,您必须在Rest Service中更改JSON响应。
或者添加一个新的类结构,比如这个
public class UserItem {
User user;
//the usual setter getter
}
然后休息呼叫将是这样的:
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
UserItem item = restTemplate.getForObject(url, UserItem .class);
User user = item.getUser();