SpringBoot-Controller之统一返回类和状态码,统一异常处理,统一返回处理

x33g5p2x  于2022-01-13 转载在 Spring  
字(2.3k)|赞(0)|评价(0)|浏览(513)

Controller接口数据统一返回类和状态码

/** * 统一返回 * @param <T> */
public class Result<T> implements Serializable {
    private boolean flag;//是否成功
    private Integer code;//返回码
    private String message;//返回消息
    private T data;//返回数据

    public Result(boolean flag, Integer code, String message, Object data) {
        this.flag = flag;
        this.code = code;
        this.message = message;
        this.data = (T) data;
    }

    public Result(boolean flag, Integer code, String message) {
        this.flag = flag;
        this.code = code;
        this.message = message;
    }

    public Result() {
        this.flag = true;
        this.code = StatusCode.OK;
        this.message = "操作成功!";
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

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

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
/** * 返回码 */
public class StatusCode {
    public static final int OK = 20000;//成功
    public static final int ERROR = 20001;//失败
    public static final int LOGINERROR = 20002;//用户名或密码错误
    public static final int ACCESSERROR = 20003;//权限不足
    public static final int REMOTEERROR = 20004;//远程调用失败
    public static final int REPERROR = 20005;//重复操作
    public static final int NOTFOUNDERROR = 20006;//没有对应的抢购数据
}

拦截所有Controller异常

@RestControllerAdvice
public class RestCtlAdvice {

	// 其他异常或者自定义异常可以自行拦截,修改@ExceptionHandle即可
	注意顺序是从上到下执行的
	
	//1

	//2

    //3

	//如果以上异常都拦截不住那么这个就是最后的防线,拦截所有的异常
    @ExceptionHandler(value = Exception.class)
    public String ExceptionHandler(Exception e) {
         e.printStackTrace();
        return new Result(false, StatusCode.ERROR, e.getMessage());
    }

	

}

拦截所有Controller返回信息

@ControllerAdvice
public class UnifiedResponseBodyHandle implements ResponseBodyAdvice<Result> {
    private Logger logger = LoggerFactory.getLogger(UnifiedResponseBodyHandle.class);
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        /*支持所有方法*/
        return true;
    }


    //所有接口返回信息都会从这里出去,我们可以在这里进行一些特殊处理,或者日志记录
    @Override
    public Result beforeBodyWrite(Result body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        String methodName = returnType.getMethod().getName();
        logger.info("{}方法返回值={}", methodName, JSON.toJSONString(body));
        return body;
    }
}

点赞 -收藏-关注-便于以后复习和收到最新内容有其他问题在评论区讨论-或者私信我-收到会在第一时间回复如有侵权,请私信联系我感谢,配合,希望我的努力对你有帮助^_^

相关文章