Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

404 on static resource does not return HTML error page #31569

Closed
jorgerod opened this issue Nov 7, 2023 · 8 comments
Closed

404 on static resource does not return HTML error page #31569

jorgerod opened this issue Nov 7, 2023 · 8 comments
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: superseded An issue that has been superseded by another

Comments

@jorgerod
Copy link

jorgerod commented Nov 7, 2023

With the upgrade to Spring Boot 3.2.0-RC2 I think there is the following regression bug:

When a user visits an application, with ProblemDetail support enabled, with a browser and asking for HTML, then Spring should return the standard HTML error page instead of returning a ProblemDetail JSON or XML when static resource does not exist.
This would particularly be important for applications that both serve a browser frontend as well as a REST API.

  • Spring Boot 3.1.5

image

  • Spring Boot 3.2.0-RC2

image

Related issues:

NOTE: Attached is an example
sb3-rest-webmvc.zip

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 7, 2023
@juliojgd
Copy link

@rstoyanchev any chance this can be triaged/checked before 3.2.0 release?

@bclozel
Copy link
Member

bclozel commented Nov 21, 2023

I've had a look and reproduced the problem. This is an unfortunate consequence of #29491 and #30930 which is really a disconnect between the evolution of error handling and problem details here in Spring Framework, and how things are done in Spring Boot.
There are already some inconsistencies (see spring-projects/spring-boot#33885) and we'd like to revisit the error handling support in Spring Boot with spring-projects/spring-boot#19525. Unfortunately, I don't see how we can solve this before the Spring Boot 3.2 release due this week.

I'm leaving this issue opened for now to discuss that in the Spring Framework team first, but I think the changes should happen ultimately in Spring Boot.

@tobias-lippert
Copy link

Hi Spring Team,
I tried to update to Spring Boot 3.2 today but this regression seems to block our upgrade. We use a custom ErrorController that returns a custom response. The client uses the response to determine that an error page should be shown.

Using the global exception handler to handle the NoResourceFoundException fails because it results in an ambiguous @ExceptionHandler

  1. Is there any known workaround?
  2. Is it likely that this will be fixed with the next patch release?

@bclozel
Copy link
Member

bclozel commented Nov 24, 2023

Here is a workaround for this issue: declare the following bean in your application:

import org.springframework.core.annotation.Order;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.resource.NoResourceFoundException;

@ControllerAdvice
@Order(-1)
class NoResourceFoundExceptionHandler {

    @ExceptionHandler(NoResourceFoundException.class)
    public final ResponseEntity<Object> handleResourceNotFound(Exception ex) throws Exception {
        throw ex;
    }

}

@rstoyanchev
Copy link
Contributor

rstoyanchev commented Nov 24, 2023

Or alternatively, provide your own ResponseEntityExceptionHandler:

@ControllerAdvice
public class ProblemDetailsExceptionHandler extends ResponseEntityExceptionHandler {

    @Override
    protected ResponseEntity<Object> handleNoResourceFoundException(
            NoResourceFoundException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) {

        // If exception handling fails, processing of original exception continues 
        throw new IllegalStateException();
    }
}

@rstoyanchev
Copy link
Contributor

Before RFC 7807 we could not have an opinion in Spring Framework on the format of an error response, and could only have the abstract ResponseEntityExceptionHandler that does most of the work except the body format. Now that we are able to fill in that blank, it becomes more feasible to handle all exceptions within Spring MVC rather than letting them escape to Boot's error handling.

#29491 and #30930 bring static resource exceptions into the fold, by having those handled within Spring MVC like other internal exceptions. However, ResponseEntityExceptionHandler only supports REST API responses. We would need an alternative that also can handle requests from browsers. The issue is there is currently no good way to create exception handler method variants with ResponseEntity or ModelAndView depending on the requested content type, which is what Boot's error mechanism does by handling ERROR dispatches in BasicErrorController with @RequestMapping methods. We should consider adding a produces attribute to @ExceptionHandler.

@tobias-lippert
Copy link

@bclozel I've applied your proposed workaround today and it seems to resolve all our problems. Thanks for the quick fix. 😄

fengelniederhammer added a commit to loculus-project/loculus that referenced this issue Dec 11, 2023
We have a separate info controller that shows the link to the swagger ui.
Since Spring Boot 3.2 the 404 error handling is different (see spring-projects/spring-framework#31569), so this is effectively unused.
fengelniederhammer added a commit to loculus-project/loculus that referenced this issue Dec 11, 2023
We have a separate info controller that shows the link to the swagger ui.
Since Spring Boot 3.2 the 404 error handling is different (see spring-projects/spring-framework#31569), so this is effectively unused.
@jhoeller jhoeller added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Jan 3, 2024
@bclozel
Copy link
Member

bclozel commented Jan 3, 2024

Closing this issue in favor of #31936 on the Framework side, spring-projects/spring-boot#33885 and spring-projects/spring-boot#19525 for Spring Boot support.

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Jan 3, 2024
@bclozel bclozel added status: superseded An issue that has been superseded by another and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: superseded An issue that has been superseded by another
Projects
None yet
Development

No branches or pull requests

7 participants