Springboot 自定义简单的全局统一响应体

       一般来说控制层加的都是@RestController来表明任何一个请求都会有一个相应结果,规范的情况下都会定义一个全局的响应体,这里先展示一种简单的,有关响应码直接定义在该响应体类中:

public class AjaxResponse {

    public static final String ERROR_MSG = "操作失败";

    public static final String SUCCESS_MSG = "success";

    public static final int SUCCESS_NO = 0;

    public static final int FAILED = 500;

    private int errorNo;
    private String errorInfo;
    private Object data;

    private AjaxResponse() {
    }

    public static AjaxResponse newInstance() {
        return new AjaxResponse();
    }

    public static AjaxResponse newInstance(int errorNo, String errorInfo) {
        AjaxResponse response = new AjaxResponse();
        response.setErrorNo(errorNo);
        response.setErrorInfo(errorInfo);
        response.setData(new HashMap<String, Object>());
        return response;
    }

    public static AjaxResponse newSuccess() {
        return newInstance(SUCCESS_NO, SUCCESS_MSG);
    }

    public static AjaxResponse new403() {
        return newInstance(403, "Permission denied");
    }

    public static AjaxResponse new401() {
        return newInstance(401, "Login required");
    }

    public int getErrorNo() {
        return errorNo;
    }

    public void setErrorNo(int errorNo) {
        this.errorNo = errorNo;
    }

    public String getErrorInfo() {
        return errorInfo;
    }

    public void setErrorInfo(String errorInfo) {
        this.errorInfo = errorInfo;
    }

    public Object getData() {
        return data;
    }

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

    public void putData(String key, Object value) {
        if (data == null || !(data instanceof Map)) {
            data = new HashMap<String, Object>();
        }
        ((Map) data).put(key, value);
    }

    public void setException(Exception e) {
        if (null != e) {
            this.setErrorNo(FAILED);
            this.setErrorInfo(e.getMessage());
        }
    }
}

       使用非常简单:

//一般先在方法的开始new 一个成功的响应体变量
AjaxResponse ajax = AjaxResponse.newSuccess();
//如果需要加入响应数据,则
ajax.putData("xx",xx); //可以写多个
//如果响应数据只有一个
ajax.setData(xx);
//业务逻辑无误,返回
return ajax
//如果是一些业务异常,则
return AjaxResponse.newInstance(500,"xxxxx");
//如果是一些常规的异常
return AjaxResponse.new401();
return AjaxResponse.new403();

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以通过创建自定义异常类来实现自定义异常。以下是一个示例: ```java // 自定义异常类 public class CustomException extends RuntimeException { private String errorCode; private String errorMessage; public CustomException(String errorCode, String errorMessage) { this.errorCode = errorCode; this.errorMessage = errorMessage; } public String getErrorCode() { return errorCode; } public String getErrorMessage() { return errorMessage; } } // 异常处理类 @ControllerAdvice public class CustomExceptionHandler { @ExceptionHandler(CustomException.class) public ResponseEntity<ErrorResponse> handleCustomException(CustomException ex) { ErrorResponse errorResponse = new ErrorResponse(ex.getErrorCode(), ex.getErrorMessage()); return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); } } // 错误响应类 public class ErrorResponse { private String errorCode; private String errorMessage; public ErrorResponse(String errorCode, String errorMessage) { this.errorCode = errorCode; this.errorMessage = errorMessage; } public String getErrorCode() { return errorCode; } public String getErrorMessage() { return errorMessage; } } ``` 在上面的示例中,我们首先创建了一个自定义异常类`CustomException`,该类继承自`RuntimeException`。然后,我们创建了一个异常处理类`CustomExceptionHandler`,使用`@ControllerAdvice`注解将其标记为全局异常处理类。在`CustomExceptionHandler`中,我们使用`@ExceptionHandler`注解来指定处理`CustomException`异常的方法,并返回自定义的错误响应对象`ErrorResponse`。最后,我们创建了一个`ErrorResponse`类,用于封装错误码和错误信息。 通过以上步骤,我们就可以在Spring Boot自定义异常并进行统一的异常处理了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值