|
@@ -1,11 +1,16 @@
|
|
|
package com.chelvc.framework.base.interceptor;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.Set;
|
|
|
import java.util.stream.Collectors;
|
|
|
+import javax.servlet.Filter;
|
|
|
+import javax.servlet.FilterChain;
|
|
|
+import javax.servlet.ServletRequest;
|
|
|
+import javax.servlet.ServletResponse;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.validation.ConstraintViolation;
|
|
@@ -29,6 +34,8 @@ import org.springframework.beans.TypeMismatchException;
|
|
|
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
|
|
|
import org.springframework.boot.web.error.ErrorAttributeOptions;
|
|
|
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
|
|
|
+import org.springframework.core.Ordered;
|
|
|
+import org.springframework.core.annotation.Order;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.converter.HttpMessageConversionException;
|
|
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
|
@@ -61,7 +68,8 @@ import org.springframework.web.multipart.support.MissingServletRequestPartExcept
|
|
|
@Slf4j
|
|
|
@Controller
|
|
|
@RestControllerAdvice
|
|
|
-public class GlobalExceptionInterceptor extends AbstractErrorController {
|
|
|
+@Order(Ordered.HIGHEST_PRECEDENCE)
|
|
|
+public class GlobalExceptionInterceptor extends AbstractErrorController implements Filter {
|
|
|
private final ErrorAttributeOptions options = ErrorAttributeOptions.of(ErrorAttributeOptions.Include.values());
|
|
|
|
|
|
public GlobalExceptionInterceptor() {
|
|
@@ -73,6 +81,17 @@ public class GlobalExceptionInterceptor extends AbstractErrorController {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException {
|
|
|
+ try {
|
|
|
+ chain.doFilter(request, response);
|
|
|
+ } catch (Exception e) {
|
|
|
+ LoggingContextHolder.warn(log, request, e);
|
|
|
+ SessionContextHolder.response((HttpServletResponse) response, HttpStatus.BAD_REQUEST,
|
|
|
+ ApplicationContextHolder.getMessage("Request.Invalid"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 参数绑定结果格式化
|
|
|
*
|