httpClient調用接口的時候,解析返回報文內容


比如我httpclient調用的接口返回的格式是這樣的:

一:data里是個對象

{
    "code": 200,
    "message": "執行成功",
    "data": {
        "userId": 1,
        "userName": "Jamie",
        "roles": "admin",
        "realName": "測試人員",
        "tel": "13651046002",
        "email": "12345@qq.com",
        "enabled": true
    }
}

 

1.添加pom依賴

<!--集成lombok-->
<dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
     <optional>true</optional>
</dependency>

<!-- 集成json-->
<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version>1.2.58</version>
</dependency>

 

2.定義一個返回報文實體

/**
 * @author: sujing
 * @version: 1.0.0
 * @ClassName: Return.java
 * @date: 2019/10/11 16:24
 * 返回報文封裝
 */
@Data
public class Return<T> {
    String code;
    String message;
    T data;

    public Return() {
    }

    public Return(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public Return(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
}

 

3.解析httpclient返回的參數

//httpClient調用接口,返回String字符串
String httpClientReturn = HttpClientUtil.get("http://xxxxx:9099/jwtauthsvr/api/getUserInfoByName", request.getHeader("Authorization"));

//轉成JsonObject
JSONObject UserInfoReturn = JSON.parseObject(httpClientReturn);

//轉成自定義Return報文對象
Return returnMsg = JSON.toJavaObject(UserInfoReturn, Return.class);

//獲取報文里data參數
String JSONData = JSON.toJSONString(returnMsg.getData());

//轉成UserInfo對象
UserInfo userInfo = JSON.parseObject(JSONData, UserInfo.class);

 

二:data里是個集合

{
    "code": 200,
    "message": "success",
    "data": {
        "total": 16,
        "list": [
            {
                "userId": 1,
                "userName": "Jamie",
                "roles": "admin",
                "realName": "測試人員",
                "tel": "13651046002",
                "email": "12345@qq.com",
                "idCard": "1111111111",
                "enabled": true,
                "relType": 0,
                "relId": 111098,
                "password": "xxx",
                "registrationTime": "2019-08-04 20:35:44",
                "updateTime": "2019-08-04 20:35:44"
            },
            {
                "userId": 2,
                "userName": "Bruce",
                "roles": "admin",
                "realName": "測試人員2",
                "tel": "13651046888",
                "email": "wwee@qq.com",
                "idCard": "370214199555555555",
                "enabled": true,
                "relType": 0,
                "relId": 111099,
                "password": "xxx",
                "registrationTime": "2019-08-04 20:50:15",
                "updateTime": "2019-08-04 20:50:15"
            }
        ],
        "pageNum": 1,
        "pageSize": 25,
        "size": 16,
        "startRow": 1,
        "endRow": 16,
        "pages": 1,
        "prePage": 0,
        "nextPage": 0,
        "isFirstPage": true,
        "isLastPage": true,
        "hasPreviousPage": false,
        "hasNextPage": false,
        "navigatePages": 8,
        "navigatepageNums": [
            1
        ],
        "navigateFirstPage": 1,
        "navigateLastPage": 1,
        "firstPage": 1,
        "lastPage": 1
    }
}

 

1.解析httpclient返回的參數

//httpClient調用接口,返回String字符串
String httpClientReturn = HttpClientUtil.get("http://xxxxx:9099/jwtauthsvr/api/getUserInfoByName", request.getHeader("Authorization"));
//轉成JsonObject JSONObject UserInfoReturn = JSON.parseObject(httpClientReturn); //獲取返回報文格式 Return returnMsg = JSON.toJavaObject(UserInfoReturn, Return.class); //獲取報文里data參數 String JSONData = JSON.toJSONString(returnMsg.getData()); //獲取data里的list List<UserInfo> userInfoList = JSON.parseObject(JSONData, new TypeReference<List<UserInfo>>() {});

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM