If an exception is directly annotated with @ResponseStatus(HttpStatus.NOT_FOUND) the default error page handling works and a provided template at error/404.html is rendered.
However when using a custom @ControllerAdvice for exception mapping the template isn't rendered because it's now the developers responsibility.
What I want is a mechanisms which just allows me to map any exception (which I can not or do not want to annotate) to a HTTP status code and let the default error handling take over the template resolution.
The application at https://github.com/mvitz/spring-boot-exception-handling can be used to demonstrate the current behaviour.
Comment From: mvitz
I just found #7329 which is in some way related.
Comment From: bclozel
Indeed, this is a duplicate of #7329 - closing this now.
Comment From: philwebb
I'm not sure that this is exactly a duplicate of #7329. It would be nice to do something to help with this (even if it's just better documentation).
Comment From: ZaitsevY
@ExceptionHandler(CustomException.class)
void handleCustomExcepption(HttpServletResponse response) throws IOException {
response.sendError(HttpStatus.NO_CONTENT.value());
}
~~won't produce~~ default error message as of Spring Boot 1.5.5
Update: my own fault (or misundestanding): no response was generated for NO_CONTENT (204) status code, after switching to any other code the response is there.
Comment From: wilkinsona
@ZaitsevY You've already said that once. There's no need to spam multiple issues with the same comment.
Comment From: ZaitsevY
@wilkinsona excuse, please, I duplicated after having understood that the first comment went to the closed issue.
Comment From: bclozel
I think this has been superseded by the ProblemDetail support in #32634. This will be expanded as well in #19525.
Comment From: mvitz
@bclozel I'm uncertain if this is really superseded by the ProblemDetail support because the original problem is still present with Spring Boot 3.0.2.
Given the following Spring Boot 3.0.2 application
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@ResponseStatus(NOT_FOUND)
public static class CustomExceptionWithResponseStatusAnnotation extends RuntimeException {}
public static class CustomExceptionWithoutResponseStatusAnnotation extends RuntimeException {}
@ControllerAdvice
public static class ErrorHandler {
@ExceptionHandler(CustomExceptionWithoutResponseStatusAnnotation.class)
@ResponseStatus(NOT_FOUND)
public void handleCustomExceptionWithoutResponseStatusAnnotation() {}
}
@RestController
public static class MyController {
@GetMapping("/withAnnotation")
public String withAnnotation() {
throw new CustomExceptionWithResponseStatusAnnotation();
}
@GetMapping("/withoutAnnotation")
public String withoutAnnotation() {
throw new CustomExceptionWithoutResponseStatusAnnotation();
}
}
}
Hitting /withoutAnnotation which throws the Exception handled by my custom ControllerAdvice does not return a body although the /withAnnotation which throws the Exception that is directly annotated with @ResponseStatus works and returns the problem details JSON.
Comment From: bclozel
@mvitz You're right, this will be dealt with #33885