原因:在Spring Boot由于全局异常处理@RestControllerAdvice只会去捕获所有Controller层抛出的异常,所以在filter当中抛出的异常异常类是没有感知的
解决方案:添加CustomExceptionFilter过滤器
package com.zy.basy.common.core.filter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.servlet.HandlerExceptionResolver;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class CustomExceptionFilter extends OncePerRequestFilter {
@Autowired
@Qualifier("handlerExceptionResolver")
private HandlerExceptionResolver resolver;
@Override
protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull FilterChain filterChain) throws ServletException, IOException {
try {
filterChain.doFilter(request, response);
} catch (Exception e) {
resolver.resolveException(request, response, null, e);
原因:在Spring Boot由于全局异常处理@RestControllerAdvice只会去捕获所有Controller层抛出的异常,所以在filter当中抛出的异常异常类是没有感知的解决方案:添加CustomExceptionFilter过滤器package com.zy.basy.common.core.filter;import org.springframework.beans.factory.annotation.Autowired;import org.springfram
<groupId>io.sprucehill</groupId>
<artifactId>spring-cors-filter</artifactId>
<version>1.0.0</version>
</dependency>
Gradle
compile 'io.sprucehill.spring-cors-filter:1.0.0
启用bean
如果您使用的是JavaConfig和ComponentScan,请确保已扫描io.sprucehill.spring.filter并将至少以下属性添加到applicat
@Slf4j
@RestControllerAdvice( annotations = RestController.class )
public class ApiExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(...
OncePerRequestFilter是什么?
OncePerRequestFilter是SpringBoot提供的一种能够确保在一次请求只通过一次filter,一般情况下我们要自定义Filter都会继承这个类。
What!!! 一次请求还会过滤多次吗?
Filter base class that...