I tried to implement a filter following this answer:
How do I minify dynamic HTML responses in Spring?
This works very good, however, this filter does not work when the error 404 is thrown. Why not? And how do I apply this filter also for my 404-error page?
Solution:
Using the following code:
@ControllerAdvice
@Order(HIGHEST_PRECEDENCE)
public class NotFoundException {
@ExceptionHandler(NoHandlerFoundException.class)
public String noHandlerFoundException() {
// provide information to view
return "error";
}
}
with the application.properties:
spring.web.resources.add-mappings=false
spring.mvc.throw-exception-if-no-handler-found=true
allows the redirection to the error page and the filter is applied. However, the warning: "No mapping for GET /css/styles.css" (and all other static resources) is logged. Registering a ResourceHandler
with:
resourceHandlerRegistry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/");
// register resource handler for other static content
solved this. See also issues: #7653 (spring-boot) and #29491 (spring-framework) in the spring-projects, which seem related. Thank you!