处理程序
@ControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(value=Exception.class)
public Response<String> exceptionHandler(HttpServletRequest request, Exception e) {
Log logger=new Log();
logger.setCreatedAt(LocalDateTime.now());
// logger.setApiName();
// logger.setData();
// logger.setResult();
if (e instanceof GlobalException) {
GlobalException ex = (GlobalException) e;
return Response.error(ex.getMessage());
} else if (e instanceof BindException) {
BindException ex = (BindException) e;
List<ObjectError> errors = ex.getAllErrors();
ObjectError error = errors.get(0);
String msg = error.getDefaultMessage();
return Response.error(msg);//to do
} else {
return Response.error(errorSys.getMsg());
}
}
例外
public class GlobalException extends RuntimeException {
private ApiHttpCode apiHttpCode;
public GlobalException(ApiHttpCode apiHttpCode){
super(apiHttpCode.toString());
this.apiHttpCode=apiHttpCode;
}
public ApiHttpCode getApiHttpCode(){
return apiHttpCode;
}
}
log
public class Log {
private String id;
private String apiName;
private String data;
private String result;
private String requestId;
private LocalDateTime createdAt;
}