如何对由RestTemplate返回的ResponseEntity对象进行反序列化操作?

huangapple go评论82阅读模式
英文:

How to unmarshall a ReponseEntity Object returned by RestTemplate?

问题

以下是您要翻译的内容:

我有两个第三方API,我正在访问并使用RestTemplate获得ResponseEntity<Object>作为响应。我想要将响应解组为对象,以便我可以访问其中的内容。

ResponseEntity<Object> response = restTemplate.exchange(apiEndPointToHit, HttpMethod.GET, null, Object.class);

第一个API的响应

{
    "responseStatus": "SUCCESS",
    "size": 88,
    "start": 0,
    "limit": 200,
    "sort": "id asc",
    "users": [
        {
            "user": {
                "user_name": "XYZ",
                "user_first_name": "XYZ",
                "user_last_name": "XYZ",
                "user_email": "XYZ"
            }
        },
        {
            "user": {
                "user_name": "ABC",
                "user_first_name": "ABC",
                "user_last_name": "ABC"
            }
        },
        {
            "user": {
                "user_name": "PQR",
                "user_first_name": "PQR",
                "user_last_name": "PQR"
            }
        }
    ]
}

在这个响应中,"users" 将包含多个用户。我想要获取所有用户的列表,以便我可以访问列表中每个用户对象的字段,如 user_name、user_first_name 等。

第二个API的响应是

{
    "message": {
        "documents": {}
    }
}

在这种情况下,一个消息将有一个文档。我想要访问这个文档。
JSON响应包含多个字段。因此,不可能创建一个POJO类。有没有办法在不创建POJO类的情况下访问响应实体中的对象?

英文:

I have two third party API's which I'm hitting and getting the response as ResponseEntity&lt;Object&gt; using RestTemplate. I want to unmarshal the response into objects so that I can access the content inside of it.

ResponseEntity&lt;Object&gt; response = restTemplate.exchange(apiEndPointToHit,HttpMethod.GET,null,Object.class);

1st API Response

    {
        &quot;responseStatus&quot;: &quot;SUCCESS&quot;,
        &quot;size&quot;: 88,
        &quot;start&quot;: 0,
        &quot;limit&quot;: 200,
        &quot;sort&quot;: &quot;id asc&quot;,
        &quot;users&quot;: [
            {
                &quot;user&quot;: {
                    &quot;user_name&quot;: &quot;XYZ&quot;,
                    &quot;user_first_name&quot;: &quot;XYZ&quot;,
                    &quot;user_last_name: &quot;XYZ&quot;,
                    &quot;user_email&quot;: &quot;XYZ&quot;,
                  }
            },

           {
                &quot;user&quot;:{
                   &quot;user_name&quot;:&quot;ABC&quot;,
                   &quot;user_first_name&quot;:&quot;ABC&quot;,
                   &quot;user_last_name&quot;:&quot;ABC&quot;,
                 },
             },

             {
               &quot;user&quot;:{
                   &quot;user_name&quot;:&quot;PQR&quot;,
                   &quot;user_first_name&quot;:&quot;PQR&quot;,
                   &quot;user_last_name&quot;:&quot;PQR&quot;,
                }
             },

           }
         ]
    }

In this response, users will contain multiple user. I want to get list of all user. So that I can access the content of each user object inside that list fields like user_name, user_first_name, etc.

2nd API response is

{
  &quot;message&quot;: {
    &quot;documents&quot;: {
   }
 }
}

In this case, one message will have one document. I want to access the document.
JSON responses contain multiple fields. So it is not possible to create a POJO class.
Is there any way to access the Objects inside response entity with creating POJO classes?

答案1

得分: 1

我们可以使用对象映射器(Object Mapper)或 JSONObject 来帮助反序列化 JSON 主体。我建议使用 JSONObject,并按照以下示例来对 jsonbody 进行拆分。

1. 添加依赖

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20180130</version>
</dependency>
  1. 初始化 JSONObject,将 jsonResponse 传递给它。
JSONObject jo = new JSONObject(jsonResponse);
  1. 通过以下方式尝试访问 jsonResponse 中的对象
getJsonObject/getJsonArray

JSONArray ja = jo.getJsonArray("users");
英文:

We can use either Object mapper or JSONObject which helps to deserialize the JSON body. I would suggest to use JSONObject and following the below example to unmarshall jsonbody.

1. Add Dependency

&lt;dependency&gt;
    &lt;groupId&gt;org.json&lt;/groupId&gt;
    &lt;artifactId&gt;json&lt;/artifactId&gt;
    &lt;version&gt;20180130&lt;/version&gt;
&lt;/dependency&gt;
  1. Initialize the JSONObject passing the jsonReponse from the restTemplate.
JSONObject jo = new JSONObject(jsonResponse);
  1. Try acceessing the objects in jsonResponse by just doing
getJsonObject/getJsonArray

JSONArray ja = jo.getJsonArray(&quot;users&quot;);

huangapple
  • 本文由 发表于 2020年8月21日 21:36:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63523930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定