当前位置: 首页 > 知识库问答 >
问题:

RequestMappingHandlerMapping和WebMvcConfigurerAdapter

师曦
2023-03-14

我用Spring靴。

class Foo {
    private final RequestMappingHandlerMapping mapping;

    Foo(RequestMappingHandlerMapping mapping) {
      this.mapping = mapping;
    }
}
@Configuration
class Bar extends RepositoryRestMvcConfiguration {

    @Bean
    public Foo foo(){
       return new Foo(requestMappingHandlerMapping());
    }

    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping(){
       return new RequestMappingHandlerMapping();
    }
}
@Configuration
class Baz extends WebMvcConfigurerAdapter {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(<some interceptor>).addPathPatterns(URL_PATTERN);
    }
}

共有1个答案

诸葛苏燕
2023-03-14

我修复了部分问题,这是由@enablewebmvc导致POST返回415而不是201所引起的

@Configuration
class Baz extends WebMvcConfigurerAdapter {

   @Override
   public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(<some interceptor>).addPathPatterns(URL_PATTERN);
   }

   @Override
   public void configureMessageConverters(List<HttpMessageConverter<?>>converters) {
   converters.add(new MappingJackson2HttpMessageConverter());
   }
}

看起来现在一切都已经就绪:RequestMappingHandlerMapping正在被拾取,拦截器也已正确注册。

 类似资料: