Spring MVC can wrongly return 405 http code when using annotated controllers with other handler mapping methods (router functions for example) #31563
Labels
in: web
Issues in web modules (web, webmvc, webflux, websocket)
status: declined
A suggestion or change that we don't feel we should currently apply
This issue stands for Spring framework 5.3.30 (spring-webmvc artifact) but the code seems to be the same in Spring 6
I use together annotated controllers and functional endpoints to handle http request in spring MVC and it happens that Spring MVC returns an 405 http code (Method Not Allowed) which is not expected.
For example : I declare an @PutMapping method in an annotated controller (class PersonController) to handle
PUT /persons/{id}
and I declare a functional endpoint (configuration class PersonConfig) to handleGET /persons/{id}
. When I requestGET /persons/{id}
, the application returns a 405 http code (Method Not Allowed) because according to the annotated controller, the pattern /persons/{id} is only processed with PUT. That is not what is expected : when requested withGET /persons/{id}
the application is expected to process the request with the methodpersonHandler
.It happens because the implementation of
handleNoMatch
inRequestMappingInfoHandlerMapping
seems to break the contract of AbstractHandlerMethodMapping.lookupHandlerMethod : when a request doen't match, handleNoMatch may raise an exception to explain why it doesn't match and so doesn't return null. Therefore lookupHandlerMethod doesn't return null as expected if no match. Then the DispatcherServlet stops searching in others handlerMapping if there is a match for the request.In such a case, maybe exceptions like
HttpRequestMethodNotSupportedException
should be treated as null value returns and only raised if no other handlerMapping matches.Class PersonConfig
Class PersonController
The text was updated successfully, but these errors were encountered: