Description
Nick opened SPR-14397 and commented
When two methods have the same mapping but differ on what media types they consume, handleNoMatch is throwing a 415 Unsupported media type when it should be throwing 406 Not acceptable. In the following example, we have two methods that both produce JSON, but take different input formats:
- POST /test, Input (consumes): JSON, Output (produces): JSON
- POST /test, Input (consumes): TEXT, Output (produces): JSON
@RestController
@RequestMapping(value = "/test")
public class Controller {
@RequestMapping(value = "",
method = { RequestMethod.POST },
consumes = { MediaType.APPLICATION_JSON_VALUE },
produces = { MediaType.APPLICATION_JSON_VALUE }
)
public String post1() {
return "Accept: JSON";
}
@RequestMapping(value = "",
method = { RequestMethod.POST },
consumes = { MediaType.TEXT_PLAIN_VALUE },
produces = { MediaType.APPLICATION_JSON_VALUE }
)
public String post2() {
return "Accept: TEXT";
}
}
The following requests fail with 415 when they should be 406:
- POST /test, Content-Type: JSON, Accept: application/xml
- POST /test, Content-Type: TEXT, Accept: applicaton/xml
This is because in my project, I also have an HttpMessageConverter that can produce XML. If I didn't, I would get a 406, since the sequence of events would be:
- handleNoMatch throws 415
- AbstractMessageConverterMethodProcessor attempts to convert this exception with the current message handlers
- It finds none and throws a 406 which causes the 415 to be ignored
But since there is also an HttpMessageConverter that can write XML, #3 in the above sequence succeeds and an error message is printed in XML and an HTTP status of 415 is returned.
Affects: 4.3 GA
Issue Links:
- RequestMappingInfoHandlerMapping.handleNoMatch throws HttpRequestMethodNotSupportedException incorrectly [SPR-9603] #14237 RequestMappingInfoHandlerMapping.handleNoMatch throws HttpRequestMethodNotSupportedException incorrectly