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

NoHandlerFoundException mistakenly returns request headers from ErrorResponse#getHeaders #29626

Closed
osiegmar opened this issue Dec 2, 2022 · 2 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@osiegmar
Copy link

osiegmar commented Dec 2, 2022

I found an inconsistency in 404 handling using the new ProblemDetails feature of Spring 6 (6.0.2).

My example application is:

@SpringBootApplication
@RestController
public class ProblemDetailsApplication {

    @GetMapping("/hello")
    public String hello() {
        return "World";
    }

    public static void main(String[] args) {
        SpringApplication.run(ProblemDetailsApplication.class, args);
    }

}

In order to enable ProblemDetails handling, I configured this in my application.properties:

spring.mvc.problemdetails.enabled = true
spring.mvc.throw-exception-if-no-handler-found = true
spring.web.resources.add-mappings = false

And the CLI interaction:

curl http://localhost:8080/hello
World

curl http://localhost:8080/non-existent
{"type":"about:blank","title":"Not Found","status":404,"detail":"No endpoint GET /non-existent.","instance":"/non-existent"}

curl -X POST -d '{"foo":"bar"}' http://localhost:8080/non-existent
{"timestamp":"2022-12-02T18:48:36.587+00:00","status":404,"error":"Not Found","path":"/non-existent"}

The last request did not result in a problem details report as I would expect.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 2, 2022
@osiegmar
Copy link
Author

osiegmar commented Dec 2, 2022

I figured out that a part of the problem is caused by the Content-Type. The command

curl -X POST -d '{"foo":"bar"}' http://localhost:8080/non-existent
{"timestamp":"2022-12-02T18:48:36.587+00:00","status":404,"error":"Not Found","path":"/non-existent"}

sends a Content-Type of application/x-www-form-urlencoded which causes an exception:

org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class org.springframework.http.ProblemDetail] with preset Content-Type 'application/x-www-form-urlencoded;charset=UTF-8'
	at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:319) ~[spring-webmvc-6.0.2.jar:6.0.2]

But sending a Content-Type of application/json makes things even worse:

curl -v -H "Content-Type: application/json" -d '{"foo":"bar"}' http://localhost:8080/non-existent
*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /non-existent HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.84.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 13
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 404
< host: localhost:8080
< user-agent: curl/7.84.0
< accept: */*
< Content-Type: application/json;charset=UTF-8
< Content-Length: 13
< Date: Fri, 02 Dec 2022 20:13:00 GMT
<
* Connection #0 to host localhost left intact
{"type":"abou

The response body is truncated and no exception is logged! I also noted that the Content-Length of the response is identical to the one of the request. If I change the size of the request, the size of the response changes accordingly.

@rstoyanchev rstoyanchev self-assigned this Dec 6, 2022
@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 6, 2022
@rstoyanchev rstoyanchev added this to the 6.0.3 milestone Dec 6, 2022
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Dec 6, 2022

NoHandlerFoundException already had getHeaders() which returns request headers, but in 6.0 that ends up overriding getHeaders() from ErrorResponse, and as a result the request "Content-Type" ends up being used for the response.

@rstoyanchev rstoyanchev changed the title Inconsistent 404 handling NoHandlerFoundException mistakenly returns request headers from ErrorResponse#getHeaders Dec 6, 2022
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) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants