My Spring REST API has trouble (=> 406
) when an endpoint ends with a file name, e.g. /file/example.xml
because it assumes the result should be XML but thats wrong (it should be JSON).
Written [{timestamp=Thu Oct 20 11:11:14 CEST 2016, status=406, error=Not Acceptable, exception=org.springframework.web.HttpMediaTypeNotAcceptableException, message=Could not find acceptable representation, path=/file/foobar.xml}] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@7015ebef]
I read something about a Spring 4 feature detecting the file extensions and overwritting the response type, so I tried to enforce the response type but I did not find a solution.
Here is my REST controller:
@CompileStatic
@RestController
class ImportFileController {
@RequestMapping(
value = "/file/{fileName:.+}",
method = RequestMethod.PUT,
produces = "application/json"
)
@ResponseBody
public ImportFile update(
@PathVariable("fileName") String fileName,
@RequestBody ImportFile importFile) {
// ..
return importFile
}
}
This is the request/response:
Request PUT /file/foobar.xml {"reader_name":"something"}
Response 406 {
"timestamp" : "2016-10-20 08:56:35",
"status" : 406,
"error" : "Not Acceptable",
"exception" : "org.springframework.web.HttpMediaTypeNotAcceptableException",
"message" : "Could not find acceptable representation",
"path" : "/file/foobar.xml"
}
What I tried:
- Using
{filenName:.+}
instead of{fileName}
- Using
produces
in my@RequestMapping
- Sending a custom
Accept
header (application/json
) - Setting
spring.mvc.path-matching.suffix-pattern
totrue
orfalse
but that was just a wild guess
What worked:
- Changing endpoint from
/file/{fileName:.+}
to/file/{fileName:.+}/update
But that's just a workaround, not a solution.
Important: I'm using Groovy and I'm using YML configuration.