java統一返回結果集封裝,解決No converter found for return value of type

網上很多,自己參照手寫了一個。

package com.***.pro.utils;

/**
 * @Author: wsh
 */

public class Result<T> {
    private  Boolean success;
    private  Integer code;
    private  String  messagg;
    private  T  data;

    private  Result(T data) {
        this.success=true;
        this.code=0;
        this.messagg="成功";
        this.data = data;
    }
    private Result(CodeMsg cm) {
        if(cm == null){
            return;
        }
        this.success = false;
        this.code = cm.getCode();
        this.messagg = cm.getMessage();
    }
    /**
     * 成功
     */
    public static <T> Result<T> success(T data){
        return new Result<>(data);
    }

    /**
     * 成功不需要參數
     */
    public static <T> Result<T> success(){
        return (Result<T>) success("");
    }

    /**
     * 失敗時候的調用,自定義錯誤內容
     */
    public static <T> Result<T> error(CodeMsg cm){
        return new Result<T>(cm);
    }
    /**
     * 失敗時候的調用, 自定義消息
     */
    public static <T> Result<T> error(String msg){
        CodeMsg cm= new CodeMsg(msg);
        return new Result<T>(cm);
    }







    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessagg() {
        return messagg;
    }

    public void setMessagg(String messagg) {
        this.messagg = messagg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
package com.***.pro.utils;

/**
 * @Author: wsh
 */
public class CodeMsg {
    private Integer code;
    private String message;


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

    public CodeMsg( String message) {
        this.code = -1;
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

Controller調用 

    @GetMapping("master")
    public Result master( ) {
        List<Role> roles = roleDao.queryAll(null);
        //成功
        return Result.success(roles);
    }


     @GetMapping("slave")
    public Result slave( ) {
        List<Role> roles = roleDao.queryAll(null);
        //失敗
        return Result.error("不想說話");
    }

返回結果

{
    "success": true,
    "code": 0,
    "messagg": "成功",
    "data": [
        {
            "id": "1"
        }
    ]
}

 

{
    "success": false,
    "code": -1,
    "messagg": "不想說話",
    "data": null
}

 

如果出現下面的報錯,就是你的返回類(我這裏是Result類)沒寫set、get方法導致

No converter found for return value of type: class com.***.pro.utils.Result
org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class com.***.pro.utils.Result

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章