controller层统一返回值封装类

2022-09-02 11:09:35
@Data
@Builder
public class ResultWapper<T> implements Serializable {

    private int code;
    private String msg;
    private T data;

    //返回成功的包装
    public static ResultWapper.ResultWapperBuilder getSuccessBuilder(){
       return ResultWapper.builder().code(StateCodeEnum.SUCCESS.getCode()).msg(StateCodeEnum.SUCCESS.getMsg());
    }

    //返回错误包装
    public static ResultWapper.ResultWapperBuilder getFailBuilder(){
        return ResultWapper.builder().code(StateCodeEnum.FAIL.getCode()).msg(StateCodeEnum.FAIL.getMsg());
    }
}

具体调用:

@PostMapping("/reg")
    public ResultWapper register(@RequestBody @Valid UserMemberDTO userMemberDTO){
        String result = umsMemberService.register(userMemberDTO);
        return ResultWapper.getSuccessBuilder().data(result).build();
    }
  • 作者:帅到被人砍啊
  • 原文链接:https://blog.csdn.net/sdmanooo/article/details/113398032
    更新时间:2022-09-02 11:09:35