Nella mia prova, utilizzare SimpleMappingExceptionResolver
non può accedere MissingServletRequestParameterException
, ho combinato @ControllerAdvice
e Filter
di fare il login.
Utilizzare ControllerAdvice
per prendere Throwable sollevato in Spring MVC Controller.
@ControllerAdvice
public class GlobalDefaultExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger("global_controller_exception_logger");
@ExceptionHandler(value = Throwable.class)
public void defaultErrorHandler(Throwable e) throws Throwable {
// If the exception is annotated with @ResponseStatus rethrow it and let
// the framework handle it.
// AnnotationUtils is a Spring Framework utility class.
if (AnnotationUtils.findAnnotation
(e.getClass(), ResponseStatus.class) != null) {
throw e;
}
// Otherwise log exception
logger.error("global controller default exception handler", e);
throw e;
}
@ExceptionHandler(MissingServletRequestParameterException.class)
public void httpBadRequest(Exception e, HttpServletRequest request) throws Exception {
StringBuffer requestURL = request.getRequestURL();
logger.warn("{} HTTP Status 400 - {}", requestURL, e.getMessage());
throw e;
}
}
Usa Filter
per catturare ulteriore eccezione.
@WebFilter(
filterName = "ExceptionLogFilter",
urlPatterns = "/*",
dispatcherTypes = {DispatcherType.REQUEST, DispatcherType.ASYNC, DispatcherType.ERROR}
)
public class ExceptionLogFilter implements Filter {
private static final Logger logger = LoggerFactory.getLogger("global_filter_exception_logger");
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
try {
chain.doFilter(request, response);
} catch (IOException | ServletException e) {
logger.error("bad thing happened during doFilter", e);
throw e;
}
}
......
}
configurazione logback
<logger name="global_controller_exception_logger" level="info"/>
<logger name="global_filter_exception_logger" level="info"/>
È possibile visualizzare my gist per il codice completo.
Quindi, quali eccezioni andranno perse con questo metodo e c'è un modo per ottenerle? –
Buon punto. Non ho usato la primavera ora da molto tempo e non ricordo. Avrei dovuto aggiungere qualche informazione in più sopra. La mia ipotesi migliore a questo punto sono le eccezioni derivate da 'RuntimeException', come ho suggerito sopra. –