3

I have a use case where my application hosts REST API and web application and we need to add custom header to REST APIs only. REST APIs are enabled through Spring Data REST. Typically we could use Servlet Filter to achieve this but we need code the logic of isolating requests to our REST API and add the custom headers. It would be nice if Spring Data REST API allows to add a default header to all the responses it generates. What are your thoughts? Don't say I am lazy :)

3 Answers 3

8

For folks looking for actual implementation details..

Interceptor

public class CustomInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        System.out.println("adding CORS headers.....");
        response.addHeader("HEADER-NAME", "HEADER-VALUE");
        return true;
    }

}

Java Configuration

@Configuration
public class RepositoryConfig extends
        RepositoryRestMvcConfiguration {

    @Override
    public RequestMappingHandlerMapping repositoryExporterHandlerMapping() {
        RequestMappingHandlerMapping mapping = super
                .repositoryExporterHandlerMapping();

        mapping.setInterceptors(new Object[] { new CustomInterceptor() });
        return mapping;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

For those people who search this question in future can refer stackoverflow.com/a/32952220/4283455 which is a better solution, I think :)
4

As Spring Data REST is built on top of Spring MVC, the easiest way is to configure a custom HandlerInterceptor as described in the reference documentation.

With Spring Data REST the easiest way is to extend RepositoryRestMvcConfiguration and override repositoryExporterHandlerMapping, call the parent method and then invoke ….setInterceptors(…) on it.

3 Comments

yes, that worked also for me, but with new version the repositoryExporterHandlerMapping method is gone and trying to tweak restHandlerMapping in similar way does not work. Using addInterceptors also doesn't work. @Oliver_Gierke, what is the new way of defining interceptors for Spring Data REST repositories?
For those people who search this question in future can refer stackoverflow.com/a/32952220/4283455 which is a better solution, I think :)
Just a little heads-up for people who found this answer: postHandle has some issues with ResponseEntity methods (response gets commited before headers get added). You should use a ControllerAdvice instead. Details over here: mtyurt.net/2015/07/20/…
1

Finally I managed to make the setup of custom interceptor working also on spring-data-rest 2.4.1.RELEASE.

@Configuration
public class RestMvcConfig extends RepositoryRestMvcConfiguration {

    @Autowired UserInterceptor userInterceptor;

    @Autowired ApplicationContext applicationContext;

    @Override
    public DelegatingHandlerMapping restHandlerMapping() {

        RepositoryRestHandlerMapping repositoryMapping = new RepositoryRestHandlerMapping(resourceMappings(), config());
        repositoryMapping.setInterceptors(new Object[] { userInterceptor }); // FIXME: not nice way of defining interceptors
        repositoryMapping.setJpaHelper(jpaHelper());
        repositoryMapping.setApplicationContext(applicationContext);
        repositoryMapping.afterPropertiesSet();

        BasePathAwareHandlerMapping basePathMapping = new BasePathAwareHandlerMapping(config());
        basePathMapping.setApplicationContext(applicationContext);
        basePathMapping.afterPropertiesSet();

        List<HandlerMapping> mappings = new ArrayList<HandlerMapping>();
        mappings.add(basePathMapping);
        mappings.add(repositoryMapping);

        return new DelegatingHandlerMapping(mappings);  
    }

}

I had to override the restHandlerMapping method, copy-paste it's content and add a line repositoryMapping.setInterceptors for adding custom interceptor, in my case the UserInterceptor.

Is there any better way?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.