SpringMVC: Inconsistent mapping behavior depending on url extension

前端 未结 2 881
耶瑟儿~
耶瑟儿~ 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:41

    I had required an extra step in my special case. I would like to add an extra information to @dirk-lachowski's great comment in case someone else needs. In my case I am importing an extension from WebMvcConfigurationSupport class

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

    If you need to override requestMappingHandlerMapping method then you also have to add m.setUseSuffixPatternMatch(false);

    @Configuration
    public class SearchWebMvcConfigurationSupport extends WebMvcConfigurationSupport {
        @Override
        @Bean
        public RequestMappingHandlerMapping requestMappingHandlerMapping() {
            RequestMappingHandlerMapping m = super.requestMappingHandlerMapping();
            m.setAlwaysUseFullPath(true); // This makes your endpoints receive full path, including servlet mapping (you probably will not need this)
            m.setUseSuffixPatternMatch(false);
            return m;
        }
    }
    

提交回复
热议问题