I am working on spring data services and am encountering some problems in custom interceptors. I used to use spring -data-rest-webmvc 2.2.0 and added an interceptor as follows.
public RequestMappingHandlerMapping repositoryExporterHandlerMapping() { RequestMappingHandlerMapping mapping = super .repositoryExporterHandlerMapping(); mapping.setInterceptors(new Object[] { new MyInterceptor() }); return mapping; }
It worked great for me. But when I upgraded to spring -data-rest-webmvc 2.3.0, I noticed that handlerMapping is hidden behind DelegatingHandlerMapping. So I tried to add an interceptor as follows.
In one of my configuration classes, I extended the RepositoryRestMvcConfiguration class and overriding its method.
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); } }
But after adding some of my repository operations (the findAll () operation in the repository) starts to fail. If I deleted these interceptors, these operations worked fine. (In this interceptor, I just authenticate the user). So I can not understand the problem here. Am I adding an interceptor incorrectly? Is there any other way to add an interceptor?
source share