切面控制器代码:
package com.example.empboxwarehousing.config;
import com.example.empboxwarehousing.common.exception.BusinessException;
import com.example.empboxwarehousing.common.response.WebResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.stream.Collectors;
/**
* 切面全局处理异常
*
* @author chp
* @date 2022-05-13
*/
@ControllerAdvice
@Slf4j
public class ExceptionConfig {
/**
* 处理系统异常
*
* @param exception 系统异常
* @return WebResult 自定义返回集合
*/
@ExceptionHandler(Exception.class)
@ResponseBody
public WebResult exception(Exception exception) {
WebResult webResult = new WebResult();
webResult.put("code", "9999");
webResult.put("message", "系统异常,请联系管理员");
log.error("系统异常Exception信息:{}", exception);
return webResult;
}
/**
* 处理自定义异常
*
* @param exception BusinessException类型,自定义异常
* @return WebResult 自定义返回集合
*/
@ExceptionHandler(BusinessException.class)
@ResponseBody
public WebResult businessException(BusinessException exception) {
WebResult webResult = new WebResult();
webResult.put("code", exception.getCode() == null ? "9999" : exception.getCode());
webResult.put("message", exception.getMessage() == null ? "系统异常,请联系管理员" : exception.getMessage());
log.error("系统异常BusinessException信息:{}", exception);
return webResult;
}
/**
* 参数校验错误
*
* @param e MethodArgumentNotValidException 参数校验异常
* @return WebResult 自定义返回集合
*/
@ExceptionHandler(value = MethodArgumentNotValidException.class)
@ResponseBody
public WebResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
WebResult webResult = new WebResult();
List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
String message = allErrors.stream().map(s -> s.getDefaultMessage()).collect(Collectors.joining(";"));
webResult.put("code", "9999");
webResult.put("message", message);
log.error("系统异常Exception信息:{}", message);
return webResult;
}
}
自定义异常
package com.example.empboxwarehousing.common.exception;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 自定义异常
*
* @author chp
* @date 2022-05-13
*/
@Data
@NoArgsConstructor
public class BusinessException extends RuntimeException {
/**
* 返回异常代码:
* 0000成功,
* 9999失败
*/
private String code;
/**
* 返回异常信息
*/
private String message;
public BusinessException(String message) {
super();
this.code = "9999";
this.message = message;
}
public BusinessException(String code, String message) {
super();
this.code = code;
this.message = message;
}
}
自定义返回集合
package com.example.empboxwarehousing.common.response;
import lombok.Data;
import java.util.HashMap;
/**
* @author chp
* @date 2022-05-13
*/
@Data
public class WebResult extends HashMap<String, Object> {
/**
* 返回集合code
*/
private static String CODE = "code";
/**
* 返回集合信息
*/
private static String MESSAGE = "message";
public WebResult() {
super();
super.put(CODE, "200");
super.put(MESSAGE, "success");
}
/**
* 成功返回
*/
public static WebResult success() {
WebResult result = new WebResult();
result.put(CODE, "200");
result.put(MESSAGE, "success");
return result;
}
/**
* 异常返回
*/
public static WebResult fail(String msg) {
WebResult result = new WebResult();
result.put(CODE, "9999");
result.put(MESSAGE, msg);
return result;
}
}