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.
Comment From: osiegmar
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.
Comment From: rstoyanchev
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.