None of the answers helped me.
I read dozens of Stackoverflow answers about 406 Not Acceptable, HttpMediaTypeNotAcceptableException, multipart file, ResponseBody, setting Accept headers, produces, consumes, etc.
We had Spring 4.2.4 with SpringBoot and Jackson configured in build.gradle:
compile "com.fasterxml.jackson.core:jackson-core:2.6.7" compile "com.fasterxml.jackson.core:jackson-databind:2.6.7"
All routes worked fine in our other controllers, and we could use GET, POST, PUT and DELETE. Then I started adding multi-user file upload capabilities and created a new controller. GET routes work fine, but our POST and DELETE did not. No matter how I tried different solutions from here on SO, I just kept getting 406 invalid ones.
Then finally I stumbled upon this answer: Spring throw HttpMediaTypeNotAcceptableException: could not find an acceptable view because of a dot in the url
Read Ranisa's answer and all the comments.
It all came down to our @RequestMapping values:
@RequestMapping(value = "/audio/{fileName:.+}", method = RequestMethod.POST, consumes="multipart/*") public AudioFileDto insertAudio(@PathVariable String fileName, @RequestParam("audiofile") MultipartFile audiofile) { return audioService.insert(fileName, audiofile); } @RequestMapping(value = "/audio/{fileName:.+}", method = RequestMethod.DELETE) public Boolean deleteAudio(@PathVariable String fileName) { return audioService.remove(fileName); }
The part {fileName:.+} In the @RequestMapping value caused 406 invalid ones in our case.
Here is the code I added from Ranis's answer:
@Configuration public class ContentNegotiationConfig extends WebMvcConfigurerAdapter { @Override void configureContentNegotiation(final ContentNegotiationConfigurer configurer) {
EDIT August 29, 2016:
We were having problems using configurer.favorPathExtension(false) : static SVG images stopped loading. After analysis, we found that Spring started sending SVG files back to the user interface with the type "application / octet-stream" of the content type instead of "image / svg + xml". We solved this by sending fileName as the request parameter, for example:
@RequestMapping(value = "/audio", method = RequestMethod.DELETE) public Boolean deleteAudio(@RequestParam String fileName) { return audioService.remove(fileName); }
We also removed configurer.favorPathExtension(false) . Another way might be to encode fileName in a way, but we have chosen the request parameter method to avoid additional side effects.