SpringBoot三部曲之Controller統一返回ResponseData

weixin_33766168發表於2019-01-24

使用SpringBoot構建WEB工程,由於其三大特性,使我們把注意力更多的放在核心功能呢的編寫上,這也要求我們寫出更具規範的程式碼。
我會從三個方面去加強我的程式碼規範

Controller統一返回

Controller請求AOP切面日誌處理(引數校驗,rsa校驗)

Controller統一異常處理

這篇我們先說Controller統一返回ResponesData<T>
如果你和你的後端同學配合寫過介面,可能會面裡下面的尷尬情況:


@RestController
@RequestMapping("example")
public class ExampleController {

    @GetMapping("str")
    public String str() {
        return "Result Type is String";
    }

    @GetMapping("map")
    @ResponseBody
    public Map<String, Object> map() {
        Map<String, Object> hashMap = new HashMap<>(1);
        hashMap.put("Result", "Map");
        return hashMap;
    }

    @PostMapping("none")
    public void none() {
        System.out.println("none");
    }


}

每一種返回的結果都不同。?????我的請求要怎麼寫。。。

如果使用了統一返回,結果會變得簡單,處理起來十分輕鬆,後端程式碼也變得及其簡潔。



@RestController
@RequestMapping("gua")
public class GuaController {
    @GetMapping("str")
    public ResponseData str() {
        return ResponseDataUtil.buildSuccess("Result String");
    }

    @GetMapping("data")
    public ResponseData data() {
        return ResponseDataUtil.buildSuccess(new User());
    }


    @GetMapping("map")
    public ResponseData map() {
        HashMap<String, Object> map = new HashMap<>(1);
        map.put("Result", "Map");
        return ResponseDataUtil.buildSuccess(map);
    }


}

其返回結果:

{"code":"0000","msg":"Result String","data":null}
{"code":"0000","msg":"請求成功","data":{"name":"DongGua","age":18}}
{"code":"0000","msg":"請求成功","data":{"Result":"Map"}}

需要返回得資料全部放到data,讓一切變得規範和簡潔。
實現統一返回非常簡單,只需要三個功能

ResponseData<T> ResponseDataUtils ResultEnum

三者得結構得非常簡單:
ResponseData<T>

public class ResponseData<T> implements Serializable {

    private String code;

    private String msg;

    private T data;


    public ResponseData(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResponseData(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResponseData(ResultEnums resultEnums) {
        this.code = resultEnums.getCode();
        this.msg = resultEnums.getMsg();
    }

    public ResponseData(ResultEnums resultEnums, T data) {
        this.code = resultEnums.getCode();
        this.msg = resultEnums.getMsg();
        this.data = data;
    }

    public ResponseData() {
    }

    public String getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

ResponseDataUtils


public class ResponseDataUtil {
    /**
     * 帶實體的統一返回
     *
     * @param data 實體
     * @param <T>  實體型別
     * @return
     */
    public static <T> ResponseData buildSuccess(T data) {
        return new ResponseData<T>(ResultEnums.SUCCESS, data);
    }

    public static ResponseData buildSuccess() {
        return new ResponseData(ResultEnums.SUCCESS);
    }

    public static ResponseData buildSuccess(String msg) {
        return new ResponseData(ResultEnums.SUCCESS.getCode(), msg);
    }

    public static ResponseData buildSuccess(String code, String msg) {
        return new ResponseData(code, msg);
    }

    public static <T> ResponseData buildSuccess(String code, String msg, T data) {
        return new ResponseData<T>(code, msg, data);
    }

    public static ResponseData buildSuccess(ResultEnums resultEnums) {
        return new ResponseData(resultEnums);
    }

    public static <T> ResponseData buildError(T data) {
        return new ResponseData<T>(ResultEnums.ERROR, data);
    }

    public static ResponseData buildError() {
        return new ResponseData(ResultEnums.ERROR);
    }

    public static ResponseData buildError(String msg) {
        return new ResponseData(ResultEnums.ERROR.getCode(), msg);
    }

    public static ResponseData buildError(String code, String msg) {
        return new ResponseData(code, msg);
    }

    public static <T> ResponseData buildError(String code, String msg, T data) {
        return new ResponseData<T>(code, msg, data);
    }

    public static ResponseData buildError(ResultEnums resultEnums) {
        return new ResponseData(resultEnums);
    }
}

ResultEnum


public enum ResultEnums {

    SUCCESS("0000", "請求成功"),
    ERROR("1111", "請求失敗"),
    SYSTEM_ERROR("1000", "系統異常"),
    BUSSINESS_ERROR("2001", "業務邏輯錯誤"),
    VERIFY_CODE_ERROR("2002", "業務引數錯誤"),
    PARAM_ERROR("2002", "業務引數錯誤");

    private String code;
    private String msg;

    ResultEnums(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

感興趣得小夥伴,快去實現一下吧。明天見。

相關文章