Skip to content

RequestMappingInfoHandlerMapping.handleNoMatch throws 415 when it should throw 406 [SPR-14397] #18969

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

  1. POST /test, Input (consumes): JSON, Output (produces): JSON
  2. 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:

  1. handleNoMatch throws 415
  2. AbstractMessageConverterMethodProcessor attempts to convert this exception with the current message handlers
  3. 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:

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions