" />

欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java如何對返回參數(shù)進行處理

 更新時間:2024年07月05日 09:04:49   作者:HelIanthuss  
這篇文章主要介紹了Java如何對返回參數(shù)進行處理問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

Java對返回參數(shù)進行處理

根據(jù)返回參數(shù)格式獲取其中的值

1.得到ResponseEntity<String> responseEntity對象

import org.springframework.http.ResponseEntity;

得到ResponseEntity<String> responseEntity對象

<200,

{
    "code":0,
    "data":{
        "list":[
            {
                "amount":0,
                "auditTime":"",
                "channelType":"",
                "createTime":"2019-08-13 17:01:55",
                "creditStatus":"",
                "edit":true,
                "fundsStatus":"",
                "id":372,
                "idNo":"",
                "lendRequestId":0,
                "mobile":"13289989000",
                "name":"客戶姓名",
                "soinsStatus":"",
                "state":0,
                "stateText":"",
                "viewStateText":0
            }
        ]
    },
    "mask":"251eeedb-e214-47c6-aa0c-3eb6c7b67aa0",
    "msg":"success",
    "timestamp":1566089672
}

,{Server=[Tengine/2.1.1], Date=[Sun, 18 Aug 2019 00:54:32 GMT], Content-Type=[application/json;charset=UTF-8], Content-Length=[412], Connection=[keep-alive]}>

2.根據(jù)ResponseEntity<String> responseEntity對象

獲取body部分,body為json格式字符串

String content = responseEntity.getBody();

content輸出如下:

{
    "code":0,
    "data":{
        "list":[
            {
                "amount":0,
                "auditTime":"",
                "channelType":"",
                "createTime":"2019-08-13 17:01:55",
                "creditStatus":"",
                "edit":true,
                "fundsStatus":"",
                "id":372,
                "idNo":"",
                "lendRequestId":0,
                "mobile":"13243345566",
                "name":"客戶姓名",
                "soinsStatus":"",
                "state":0,
                "stateText":"",
                "viewStateText":0
            }
        ]
    },
    "mask":"251eeedb-e214-47c6-aa0c-3eb6c7b67aa0",
    "msg":"success",
    "timestamp":1566089672
}

3.獲取list中的id,name,mobile等字段值

  • 3.1將json字符串轉化為json對象
//將json字符串轉化為json對象
JSONObject json = JSONObject.parseObject(content);

{
    "msg":"success",
    "code":0,
    "data":{
        "list":[
            {
                "amount":0,
                "soinsStatus":"",
                "viewStateText":0,
                "edit":true,
                "mobile":"12324435555",
                "channelType":"",
                "creditStatus":"",
                "fundsStatus":"",
                "idNo":"",
                "auditTime":"",
                "createTime":"2019-08-13 17:01:55",
                "stateText":"",
                "name":"客戶姓名",
                "id":372,
                "lendRequestId":0,
                "state":0
            }
        ]
    },
    "mask":"251eeedb-e214-47c6-aa0c-3eb6c7b67aa0",
    "timestamp":1566089672
}
  • 3.2 取出data部分
//取出data部分對象
JSONObject data = json.getJSONObject("data");

{
    "list":[
        {
            "amount":0,
            "soinsStatus":"",
            "viewStateText":0,
            "edit":true,
            "mobile":"13234444555",
            "channelType":"",
            "creditStatus":"",
            "fundsStatus":"",
            "idNo":"",
            "auditTime":"",
            "createTime":"2019-08-13 17:01:55",
            "stateText":"",
            "name":"客戶姓名",
            "id":372,
            "lendRequestId":0,
            "state":0
        }
    ]
}
  • 3.3 data中包含有數(shù)組

list中的內(nèi)容帶有中括號[],所以要轉化為JSONArray類型的對象

//轉化為JSONArray類型的對象
JSONArray jsonArray = data.getJSONArray("list");


[
    {
        "amount":0,
        "soinsStatus":"",
        "viewStateText":0,
        "edit":true,
        "mobile":"13234444555",
        "channelType":"",
        "creditStatus":"",
        "fundsStatus":"",
        "idNo":"",
        "auditTime":"",
        "createTime":"2019-08-13 17:01:55",
        "stateText":"",
        "name":"客戶姓名",
        "id":372,
        "lendRequestId":0,
        "state":0
    }
]
  • 3.4 若為多個數(shù)組

jsonArray.getJSONObject(index)

//隨機選取一個數(shù)組
JSONObject idInfo = jsonArray.getJSONObject(randomInteger(0,jsonArray.size()));
String id=idInfo.getString("id");

java后端常用返回參數(shù),復制粘貼直接用

@Data
public class CommonResult<T> {
    
    /**
     * 結果
     */
    private T data;
 
    /**
     * 狀態(tài)碼
     */
    private Integer code;
 
    /**
     * 狀態(tài)碼描述
     */
    private String message;
 
    public CommonResult() {}
 
    public CommonResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }
 
 
 
 
    protected CommonResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
 
    /**
     * 成功返回結果
     *
     */
    public static <T> CommonResult<T> success() {
        return new CommonResult<>(ExceptionCode.SUCCESS.getCode(), ExceptionCode.SUCCESS.getMessage());
    }
 
    /**
     * 成功返回結果
     *
     * @param data 獲取的數(shù)據(jù)
     */
    public static <T> CommonResult<T> success(T data ) {
        return new CommonResult<>(ExceptionCode.SUCCESS.getCode(), ExceptionCode.SUCCESS.getMessage(), data);
    }
 
 
 
    /**
     * 成功返回結果
     *
     * @param data 獲取的數(shù)據(jù)
     * @param  message 提示信息
     */
    public static <T> CommonResult<T> success(T data, String message) {
        return new CommonResult<>(ExceptionCode.SUCCESS.getCode(), message, data);
    }
 
    /**
     * 失敗返回結果
     * @param errorCode 錯誤碼
     * @param message 錯誤信息
     */
    public static <T> CommonResult<T> failed(Integer errorCode, String message) {
        return new CommonResult<>(errorCode, message, null);
    }
 
    /**
     * 失敗返回結果
     * @param message 提示信息
     */
    public static <T> CommonResult<T> failed(String message) {
        return new CommonResult<>(ExceptionCode.FAILED.getCode(), message, null);
    }
 
    /**
     * 權限過期
     */
    public static <T> CommonResult<T> unauthorized() {
        return new CommonResult<>(ExceptionCode.FAILED.getCode(), "用戶登錄已過期,請重新登錄!", null);
    }
 
}
public class ExceptionCode {
 
    public static final ExceptionCode SUCCESS = new ExceptionCode(200, "操作成功");
    public static final ExceptionCode FAILED = new ExceptionCode(500, "系統(tǒng)異常");
 
    private int code;
    private String message;
 
    public ExceptionCode(int code, String message) {
        this.code = code;
        this.message= message;
    }
 
    public ExceptionCode(String message) {
        this.message = message;
    }
 
    public Integer getCode() {
        return this.code;
    }
 
    public String getMessage() {
        return this.message;
    }
 
}

總結

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Java中的System.getProperty()詳解

    Java中的System.getProperty()詳解

    System.getProperty("XXX")方法用來讀取JVM中的系統(tǒng)屬性,那么java 虛擬機中的系統(tǒng)屬性使用在運行java程序的時候java -D配置,有兩種方式,一種是在命令行配置另一種是在IDE中配置,本文給大家介紹的非常詳細,感興趣的朋友一起看看吧
    2023-09-09
  • SpringBoot整合ELK實現(xiàn)日志監(jiān)控

    SpringBoot整合ELK實現(xiàn)日志監(jiān)控

    這篇文章主要為大家詳細介紹了SpringBoot整合ELK實現(xiàn)日志監(jiān)控的相關知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-11-11
  • java 文件流的處理方式 文件打包成zip

    java 文件流的處理方式 文件打包成zip

    這篇文章主要介紹了java 文件流的處理方式 文件打包成zip,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • java根據(jù)當前時間獲取yyyy-MM-dd?HH:mm:ss標準格式的時間代碼示例

    java根據(jù)當前時間獲取yyyy-MM-dd?HH:mm:ss標準格式的時間代碼示例

    在Java中可以使用java.time包中的LocalDateTime類和DateTimeFormatter類來獲取并格式化當前時間為yyyy-MM-dd?HH:mm:ss的格式,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-10-10
  • Spring中bean標簽的用法詳解

    Spring中bean標簽的用法詳解

    Bean標簽一般用于配置對象交由Spring?來創(chuàng)建,這篇文章主要來和大家詳細聊聊Spring中bean標簽的用法,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-06-06
  • String.trim()消除不了空格的問題及解決

    String.trim()消除不了空格的問題及解決

    這篇文章主要介紹了String.trim()消除不了空格的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 淺談Spring Boot 開發(fā)REST接口最佳實踐

    淺談Spring Boot 開發(fā)REST接口最佳實踐

    這篇文章主要介紹了淺談Spring Boot 開發(fā)REST接口最佳實踐,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • 詳解Java分布式系統(tǒng)中一致性哈希算法

    詳解Java分布式系統(tǒng)中一致性哈希算法

    這篇文章主要介紹了Java分布式系統(tǒng)中一致性哈希算法,對算法感興趣的同學,可以參考下
    2021-04-04
  • Springboot中Jackson用法詳解

    Springboot中Jackson用法詳解

    Springboot自帶默認json解析Jackson,可以在不引入其他json解析包情況下,解析json字段,下面我們就來聊聊Springboot中Jackson的用法吧
    2025-01-01
  • SpringBoot實現(xiàn)快遞物流查詢功能(快遞鳥)

    SpringBoot實現(xiàn)快遞物流查詢功能(快遞鳥)

    本文將基于springboot2.4.0實現(xiàn)快遞物流查詢,物流信息的獲取通過快遞鳥第三方實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-10-10

最新評論