自定义一个捕捉异常的jar包,项目中只需要引入就可以拦截并做自定义的操作
新建一个项目,创建文件:
BusinessExceptionHandler.java
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.youming.shuiku.commons.base.exception;
import com.youming.shuiku.commons.exception.BusinessException;
import com.youming.shuiku.commons.response.ResponseCode;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import org.apache.dubbo.rpc.RpcException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class BusinessExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(BusinessExceptionHandler.class);
public BusinessExceptionHandler() {
}
@ExceptionHandler({BusinessException.class, Exception.class})
public ResponseEntity<?> handlerException(HttpServletRequest request, Exception ex) {
Map<String, Object> error = new HashMap(2);
if (ex instanceof BusinessException) {
error.put("code", ((BusinessException)ex).getCode());
error.put("message", ex.getMessage());
log.warn("[全局业务异常]\r\n业务编码:{}\r\n异常记录:{}", error.get("code"), error.get("message"));
} else {
BindingResult bindingResult;
String msg;
if (ex instanceof MethodArgumentNotValidException) {
MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException)ex;
bindingResult = methodArgumentNotValidException.getBindingResult();
msg = (String)bindingResult.getFieldErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).distinct().collect(Collectors.joining(","));
error.put("code", HttpStatus.BAD_REQUEST.value());
error.put("message", msg);
log.warn("[系统异常--json]\r\n业务编码:{}\r\n异常记录:{}", error.get("code"), error.get("message"));
} else if (ex instanceof BindException) {
BindException bindException = (BindException)ex;
bindingResult = bindException.getBindingResult();
msg = (String)bindingResult.getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).distinct().collect(Collectors.joining(","));
error.put("code", HttpStatus.BAD_REQUEST.value());
error.put("message", msg);
log.warn("[系统异常--表单处理]\r\n业务编码:{}\r\n异常记录:{}", error.get("code"), error.get("message"));
} else if (ex instanceof RpcException) {
error.put("code", ResponseCode.SYSTEM_RPC.code());
error.put("message", ResponseCode.SYSTEM_RPC.message());
log.warn("[系统异常--rpc]\r\n业务编码:{}\r\n异常记录:{}", error.get("code"), ex.getMessage());
} else {
error.put("code", ResponseCode.UNKNOWN.code());
error.put("message", ResponseCode.SYSTEM_UNKNOWN.message());
log.warn("[系统异常]\r\n业务编码:{}\r\n异常记录:{}", error.get("code"), ex.getMessage());
}
}
return new ResponseEntity(error, HttpStatus.OK);
}
}
然后在META-INF中:
写入依赖的标志,这样在其他的微服务中,就可以引入这个异常处理的jar包就可以直接使用了。
举个例子:
服务中引入jar包,启动类加上需要扫描的包:
在有异常的时候会直接走自己定义的异常:
成功!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/75436.html