我正在研究spring数据休息服务并面临自定义拦截器中的一些问题.之前我使用了spring-data-rest-webmvc 2.2.0并以下列方式添加了拦截器.
public RequestMappingHandlerMapping repositoryExporterHandlerMapping() {
RequestMappingHandlerMapping mapping = super
.repositoryExporterHandlerMapping();
mapping.setInterceptors(new Object[] { new MyInterceptor() });
return mapping;
}
Run Code Online (Sandbox Code Playgroud)
它对我来说非常好.但是当我升级到spring-data-rest-webmvc 2.3.0版本时,我注意到handlerMapping隐藏在DelegatingHandlerMapping之后.因此我尝试以下列方式添加拦截器.
在我的一个配置类中,我扩展了RepositoryRestMvcConfiguration类并覆盖了它的方法.
public class AppConfig extends RepositoryRestMvcConfiguration {
@Autowired ApplicationContext applicationContext;
@Override
public DelegatingHandlerMapping restHandlerMapping()
{
RepositoryRestHandlerMapping repositoryMapping = new RepositoryRestHandlerMapping(super.resourceMappings(), super.config());
repositoryMapping.setInterceptors(new Object[] { new MyInterceptor()});
repositoryMapping.setJpaHelper(super.jpaHelper());
repositoryMapping.setApplicationContext(applicationContext);
repositoryMapping.afterPropertiesSet();
BasePathAwareHandlerMapping basePathMapping = new BasePathAwareHandlerMapping(super.config());
basePathMapping.setApplicationContext(applicationContext);
basePathMapping.afterPropertiesSet();
List<HandlerMapping> mappings = new ArrayList<HandlerMapping>();
mappings.add(basePathMapping);
mappings.add(repositoryMapping);
return new DelegatingHandlerMapping(mappings);
}
}
Run Code Online (Sandbox Code Playgroud)
但在添加之后,我的一些存储库操作(存储库上的findAll()操作)开始失败.如果我删除了这个拦截器,那些操作就可以了.(在这个拦截器中我只是验证用户.)因此我无法理解这里的问题.我是以错误的方式添加拦截器吗?有没有其他方法来添加拦截器?