Issue
I have my Spring app configured to use a GET parameter for content negotiation. Code is Kotlin but would work the same in Java.
Config:
override fun configureContentNegotiation(configurer: ContentNegotiationConfigurer) {
configurer.favorParameter(true)
.parameterName("format")
.ignoreAcceptHeader(false)
.defaultContentType(MediaType.APPLICATION_JSON)
.mediaType("text/plain", MediaType.TEXT_PLAIN)
.mediaType("application/json", MediaType.APPLICATION_JSON)
.mediaType("application/rdf+xml", MediaType("application", "rdf+xml"))
}
And the following controller methods:
@GetMapping("/test", produces=["text/plain"])
fun testText() : String {
return "Hello"
}
@GetMapping("/test", produces=["application/json"])
fun testJson() : Map<String, String> {
return mapOf("hello" to "world")
}
@GetMapping("/test", produces=["application/rdf+xml"])
fun testRdf(response: HttpServletResponse) {
// dummy response, to demonstrate using output stream.
response.setContentType("application/rdf+xml")
response.outputStream.write("dummy data".toByteArray())
response.outputStream.close()
}
testRdf
returns void
and uses an output stream to send body data back.
The following works just fine:
http://localhost:8080/test?format=text/plain
gives me the plain texthttp://localhost:8080/test?format=application/json
gives me the JSON
But http://localhost:8080/test?format=application/rdf+xml
gives me an HTTP 406 and the logs say
org.apache.tomcat.util.http.Parameters : Start processing with input [format=application/rdf+xml]
o.s.web.servlet.DispatcherServlet : GET "/test?format=application/rdf+xml", parameters={masked}
.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]
o.s.web.servlet.DispatcherServlet : Completed 406 NOT_ACCEPTABLE
A debugger shows that it doesn't even call my function.
(To prove that the testRdf
handler does what's expected, I made the path unique and removed the produces
annotation - it works fine outside content negotiation and returns the body as expected.)
As far as I can tell I have indicated that my method is the right handler for that content type, and I have registered the content type correctly.
Why does Spring not consider that my handler meets the content negotiation request?
Solution
I found the answer. The parameter characters that needed to be URL encoded, so this works fine:
http://localhost:8080/test?format=application%2Frdf%2Bxml
Answered By - Joe
Answer Checked By - Mildred Charles (JavaFixing Admin)