Spring Boot 406 status code for PUT Request

Viewed 205

Code is

@RestController
@Component
@Slf4j
public class ServicesController {
 @CrossOrigin
    @PutMapping(
            consumes = "multipart/form-data",
            path = "/{id}/{route}/structure_article/{filename:.+}")
    public ResponseEntity<ServiceResponse> updateStructureXMLFile(
            @PathVariable("id") final String id,
            @PathVariable("route") final String route,
            @RequestParam("file") final MultipartFile uploadfile,
            @PathVariable("filename") final String fileName) throws IOException {
(Some processing)
return new ResponseEntity<>(response, httpHeaders, HttpStatus.CREATED);
}
}

Here response is a POJO with public getters and setters. enter image description here

When I am putting a file getting this error:

{
    "timestamp": 1596783608973,
    "status": 406,
    "error": "Not Acceptable",
    "exception": "org.springframework.web.HttpMediaTypeNotAcceptableException",
    "message": "Could not find acceptable representation",
    "path": "/7f3033d7-3979-45e0-9f0a-172b60568edb/articles/structure_article/manuscript.xml"
}

What can be solution for this? Thank you

1 Answers

In my case, the Content Negotiation was configured like:

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer
        .favorPathExtension(true)
        .defaultContentType(MediaType.APPLICATION_JSON)
        .mediaType("xml", MediaType.APPLICATION_XML) 
        .mediaType("json", MediaType.APPLICATION_JSON); 
}

and just like you, I have a controller method with

@PutMapping("/user/{userId:.+}")

So, Spring tries to treat a request /user/email@domain.com for example with the Content-Type of .com file extension which does not exist in the Common MIME types, hence the 406 error.

I resolved it by turning off favorPathExtension.

Related