如果要从过滤器中抛出CustomException,我想使用异常处理程序来返回新的响应并打破原始的处理程序响应。
我尝试了以下代码,但未调用处理程序方法。
private boolean istest = true;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws ServletException, IOException {
KPICounterHelper.incrementRequestCounter(request);
if(istest) {
throw new CustomException("kd ex",55);
}
chain.doFilter(request, response);
KPICounterHelper.incrementResponseCounter(request);
}
//this is handler code
@RestControllerAdvice
public class ExceptionHandler extends ResponseEntityExceptionHandler{
@org.springframework.web.bind.annotation.ExceptionHandler(value=
{CustomException.class})
public CustomException handleCustom() {
System.out.println("in handler");
return new CustomException("kd excccc", 565);
}
}
//application.properties
spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=fals
我想执行处理程序代码并从那里返回响应。 并想在chain.doFilter()
之后执行方法代码