目录
1.定义异常枚举类
说明:枚举类用来存放会出现异常信息,方便服务中多次使用和快速查找(下面为示例)
package com.hhmt.delivery.utils.exception;
/**
* 辉煌明天
* FileName: ErrorCode
* Author: huachun
* email: huachun_w@163.com
* Date: 2021/11/22 17:13
* Description: 错误码
*/
public enum ErrorCode {
PARAMS_NOT_FOUND("CPA.0010001", "Important parameters cannot be found!"),
REQ_PARAMS_EXCEPTION("CPA.0010011", "Request parameter exception!"),
RES_EXCEPTION("CPA.0010012", "In response to abnormal!");
private String code;
private String 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;
}
ErrorCode(String code, String msg) {
this.code = code;
this.msg = msg;
}
}
2.定义异常结果响应类
异常信息响应类,可以通过构造器赋值
package com.hhmt.delivery.utils;
import com.hhmt.delivery.utils.exception.ServiceCustomerException;
/**
* 辉煌明天
* FileName: ErrorInfo
* Author: huachun
* email: huachun_w@163.com
* Date: 2021/12/1 19:58
* Description: ErrorInfo
*/
public class ErrorInfo {
private String code;
private String 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;
}
public ErrorInfo(ServiceCustomerException exception) {
this.code = exception.getCode();
this.msg = exception.getMsg();
}
}
3.自定义异常类
package com.hhmt.delivery.utils.exception;
import lombok.Data;
/**
* 辉煌明天
* FileName: ServiceCustomerException
* Author: huachun
* email: huachun_w@163.com
* Date: 2021/12/2 15:28
* Description:
*/
@Data
public class ServiceCustomerException extends RuntimeException {
private String code;
private String msg;
public ServiceCustomerException(ErrorCode errorCode) {
super();
this.code = errorCode.getCode();
this.msg = errorCode.getMsg();
}
}
4.配置控制层异常拦截
配置拦截器拦截服务自定义异常
package com.hhmt.delivery.handler;
import com.hhmt.delivery.utils.ErrorInfo;
import com.hhmt.delivery.utils.exception.ServiceCustomerException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
/**
* 辉煌明天
* FileName: ControllerAdviceProcessor
* Author: huachun
* email: huachun_w@163.com
* Date: 2021/12/2 15:22
* Description:
*/
@RestControllerAdvice
public class ControllerAdviceProcessor {
@ExceptionHandler(ServiceCustomerException.class)
public ErrorInfo handleException(HttpServletRequest request, ServiceCustomerException ex) {
return new ErrorInfo(ex);
}
}
5.测试
创建一个简单的控制器
package com.hhmt.delivery.test;
import com.hhmt.delivery.utils.exception.ErrorCode;
import com.hhmt.delivery.utils.exception.ServiceCustomerException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 辉煌明天
* FileName: TestExceptionController
* Author: huachun
* email: huachun_w@163.com
* Date: 2021/12/20 14:28
* Description:
*/
@RestController
@RequestMapping(value = "/test-ex")
public class TestExceptionController {
@GetMapping(value = "/ex-01")
public <T> T testEx() {
try {
System.out.println(1 / 0);
} catch (Exception exception) {
System.out.println(exception);
throw new ServiceCustomerException(ErrorCode.RES_EXCEPTION);
}
return null;
}
}
通过postman访问可以看到就会返回枚举类信息