spring-framework: Prefer problem detail media type independent of ordering in Accept header
While replacing a custom implementation of Problem Details by the new one that came with Spring 6 I was puzzled by how content negotiation is implemented. I’m currently using Spring Boot 3.0.0 with Spring 6.0.2.
I’m getting a Content-Type application/problem+json as expected when sending no accept header or Accept: */* or Accept: application/problem+json, application/json.
I also expected to receive a Problem Details JSON with Content-Type application/problem+json when sending Accept: application/json. But that might be correct - it doesn’t seem to be clearly defined by RFC 7807.
Clearly a bug, seems that I’m still getting Content-Type application/json when sending Accept: application/json, application/problem+json. See also:
Message converters and encoders indicate a preference for
application/problem+jsonwhen ProblemType is serialized. This ensuresapplication/problem+jsonis preferred when the client is flexible or has no preference.
from: https://github.com/spring-projects/spring-framework/issues/28189#issuecomment-1121378986
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15 (6 by maintainers)
Commits related to this issue
- Always use application/problem+json with ProblemDetail See gh-gh-29588 — committed to spring-projects/spring-framework by rstoyanchev 4 years ago
- Fix StringIndexOutOfBoundsException Closes gh-29588 — committed to spring-projects/spring-framework by rstoyanchev 4 years ago
Thanks for your feedback and fix @rstoyanchev! I gave 6.0.5-SNAPSHOT a try and now almost all my tests are green.
Now waiting for related https://github.com/spring-projects/spring-boot/issues/33716 to be fixed to solve the others. 😉
This is getting quite complex. I think we should separate issues.
application/problem+jsonfor JSON documents orapplication/problem+xmlfor XML documents – exclusively. This is what the RFC says and why I called the current Spring implementation “broken”.application/problem+jsonnorapplication/problem+xml.application/problem+jsonorapplication/problem+xmlin order to send a Problem Details report. In practice this almost never happens. We already explicitly enabled Problem Details use viaspring.mvc.problemdetails.enabled.application/problem+xmlif the client requested a XML document (Accept header value of*/*xml*).The Problem Details XML format is invalid per RFC I created https://github.com/spring-projects/spring-framework/issues/29927 for that.
@ueberfuhr raised several concerns/ideas/questions regarding Content Negotiation for implicit/explicit error responses. I’d suggest you also create a separate issue for that and link it to this one.