spring-projects / spring-boot

Spring Boot
https://spring.io/projects/spring-boot
Apache License 2.0
74.61k stars 40.56k forks source link

[Spring Boot 3.2.0] 404 Not Found behavior #38733

Closed infoShare closed 9 months ago

infoShare commented 9 months ago

404 Not found behavior was changed during upgrade from 3.1.6 to 3.2.0.

Previous behavior 404 Not found error returned:

{
    "timestamp": "2023-12-11T13:26:38.174+00:00",
    "status": 404,
    "error": "Not Found",
    "path": "/api/method"
}

Current behavior: NoResourceFoundException thrown with message "No static resource api/method." thrown.

Here you can find small example: https://github.com/infoShare/code-examples/tree/main/spring-boot-404

Test ApiControllerWithProfileTest is passing on spring boot 3.1.6

[INFO] Results:
[INFO] 
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

but failing on spring boot 3.2.0:

[ERROR] Failures: 
[ERROR]   ApiControllerWithProfileTest.executeMethod:40 
expected: 404 NOT_FOUND
 but was: 500 INTERNAL_SERVER_ERROR
[INFO] 
[ERROR] Tests run: 3, Failures: 1, Errors: 0, Skipped: 0
mhalbritter commented 9 months ago

Starting with Spring Framework 6.1 (which Boot 3.2.0 uses), NoResourceFoundException is thrown if there's no handler. This exception is handled by your infoshare.springboot404.handler.GlobalExceptionHandler#globalExceptionHandler and translated to a HTTP 500.

See here for more details: https://github.com/spring-projects/spring-framework/wiki/Upgrading-to-Spring-Framework-6.x#web-applications or this issue https://github.com/spring-projects/spring-framework/issues/29491.

BruceAn1978 commented 9 months ago

Starting with Spring Framework 6.1 (which Boot 3.2.0 uses), NoResourceFoundException is thrown if there's no handler. This exception is handled by your infoshare.springboot404.handler.GlobalExceptionHandler#globalExceptionHandler and translated to a HTTP 500.

See here for more details: https://github.com/spring-projects/spring-framework/wiki/Upgrading-to-Spring-Framework-6.x#web-applications or this issue spring-projects/spring-framework#29491.

No any exceptions throwed under no any ExceptionHandler configed (version: 3.1.5).

mhalbritter commented 9 months ago

I don't understand. What's the expected outcome and what does happen? Also Boot 3.1.5 doesn't use Spring Framework 6.1.0.

BruceAn1978 commented 9 months ago

I have not configure any global ExceptionHandler, a json responsed like below and no any exception was threw on server sider: { "timestamp": "2023-12-15 09:11:15", "path": "/time", "status": 404, "error": "Not Found", "requestId": "8a3f3980-1" }

It should throw a exception and can be caught by global ExceptionHandler, then the response can be adapted to some customized content.

bclozel commented 9 months ago

@BruceAn1978 So this is different from the issue reported here? The project provided by @infoShare has @ExceptionHandler methods contributed by a controller advice. If this is a different problem, please create a new issue with a minimal sample application.

BruceAn1978 commented 9 months ago

@bclozel https://github.com/spring-projects/spring-boot/issues/38826