SpringMVC: Inconsistent mapping behavior depending on url extension

前端 未结 2 890
耶瑟儿~
耶瑟儿~ 2020-12-16 21:04

I have a RESTful spring based endpoint to get assets stored in a db to a javascript editor. The relevant parts boil down to:

@RestController
@RequestMapping(         


        
2条回答
  •  庸人自扰
    2020-12-16 21:22

    It's not a bug, it's a feature...

    As @axtavt and @rhinds have supposed, something is messing around which the content type. The browsers sends a correct Accept: application/json but spring ignores this and uses the extension of the url (aarrgh). From the docs:

    16.16.4 Configuring Content Negotiation

    You can configure how Spring MVC determines the requested media types from the client for
    request mapping as well as for content negotiation purposes. The available options are to 
    check the file extension in the request URI, the "Accept" header, a request parameter, as 
    well as to fall back on a default content type. By default, file extension in the request 
    URI is checked first and the "Accept" header is checked next.
    

    The solution is quite simple as you can disable this "feature":

    @Configuration
    @EnableWebMvc
    public class RestCommonsMvcConfig extends WebMvcConfigurerAdapter {
    
        @Override
        public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
            configurer.favorPathExtension(false);
        }
    
    }
    

    See also Spring does not ignore file extension for xml-config.

    Related

    • Spring MVC @PathVariable with dot (.) is getting truncated

提交回复
热议问题