简体   繁体   English

使用Spring&Jackson将从Oracle Apex检索到的Json转换为POJO的异常

[英]Exception converting a Json retrieved from Oracle Apex to a POJO with Spring & Jackson

we have a rest web service with Oracle 11g and Apex on the server side. 我们在服务器端具有Oracle 11g和Apex的其余Web服务。 On the client side we are developing for android, and using Spring 1.0.1 and Jackson 2.2.3 libraries to manage the requests of the rest webservices and convert the json data back into a pojo. 在客户端,我们正在为android开发,并使用Spring 1.0.1和Jackson 2.2.3库来管理其余Web服务的请求,并将json数据转换回pojo。

It works very well when the webservice is a "query". 当Web服务是“查询”时,它工作得很好。 The resultset-Json data is converted in an array of Pojos without problem. 结果集-Json数据毫无问题地转换为Pojos数组。 But when we try to do the same with a oracle procedure, it fails with an exception. 但是,当我们尝试对oracle过程执行相同操作时,它会失败并出现异常。

The Json data returned by the procedure is the following: 该过程返回的Json数据如下:

{"item":"{\r\n  \"user\" : \"{john}\",\r\n  \"profile\" : \"nothing\"\r\n}"}

I tried an online Json validator, and the Json data appears to be valid. 我尝试了在线Json验证器,并且Json数据似乎有效。 In the header you can also see that the type is "application/json". 在标题中,您还可以看到类型为“ application / json”。

The pojo object is as follows: 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;
}

The code that calls the webservice and tries to convert json to pojo is the following (copied from the spring examples): 以下是调用Web服务并尝试将json转换为pojo的代码(从春季示例中复制):

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
User users = restTemplate.getForObject(url, User.class);

And at last, the exception when it tries to do "getForObject": 最后,尝试执行“ 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]

I tried to do the same with the Gson library instead the Jackson library, and the same exception is trown. 我尝试对Gson库而不是Jackson库执行相同的操作,并且抛出了相同的异常。 Now I'm blocked since a couple of days... 几天以来,我被封锁了...

Any ideas? 有任何想法吗? What I'm doing wrong? 我做错了什么?

Thanks in advance, 提前致谢,

The problem is with the JSON you are returning and the class you have declared. 问题在于返回的JSON和声明的类。 Your JSON structure is {"item":"{\\r\\n \\"user\\" : \\"{john}\\",\\r\\n \\"profile\\" : \\"nothing\\"\\r\\n}"} which doesn't map to the User class. 您的JSON结构为{"item":"{\\r\\n \\"user\\" : \\"{john}\\",\\r\\n \\"profile\\" : \\"nothing\\"\\r\\n}"}不会映射到User类。 The Json Structure that maps to the user class is {\\r\\n \\"user\\" : \\"{john}\\",\\r\\n \\"profile\\" : \\"nothing\\"\\r\\n} 映射到用户类的Json结构是{\\r\\n \\"user\\" : \\"{john}\\",\\r\\n \\"profile\\" : \\"nothing\\"\\r\\n}

So you will have to either change the JSON response in the Rest Service. 因此,您将不得不在Rest Service中更改JSON响应。

Or add a new class structure like this 或像这样添加新的类结构

public class UserItem {
 User user;
 //the usual setter getter
}

Then the Rest Call will be like this : 然后,Rest Call将如下所示:

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
UserItem item = restTemplate.getForObject(url, UserItem .class);
User user = item.getUser();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM