SpringBoot三部曲之Controller统一返回ResponseData

使用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;
    }
}

感兴趣得小伙伴,快去实现一下吧。明天见。

上一篇:elasticsearch插件六—— 分词 IK analyzer插件安装详解


下一篇:Redis CLUSTER 命令