仅使用SpringDataREST的RESTAPI的自定义默认头

仅使用SpringDataREST的RESTAPI的自定义默认头,spring,spring-data,spring-data-rest,Spring,Spring Data,Spring Data Rest,我有一个用例,我的应用程序承载RESTAPI和web应用程序,我们只需要向RESTAPI添加自定义头。RESTAPI是通过SpringDataREST启用的。通常,我们可以使用Servlet过滤器来实现这一点,但我们需要编写将请求隔离到RESTAPI的逻辑代码,并添加自定义头。如果SpringDataRESTAPI允许为它生成的所有响应添加一个默认头,那就太好了。你的想法是什么?不要说我懒惰:)由于Spring数据REST构建在Spring MVC之上,最简单的方法是配置自定义HandlerIn

我有一个用例,我的应用程序承载RESTAPI和web应用程序,我们只需要向RESTAPI添加自定义头。RESTAPI是通过SpringDataREST启用的。通常,我们可以使用Servlet过滤器来实现这一点,但我们需要编写将请求隔离到RESTAPI的逻辑代码,并添加自定义头。如果SpringDataRESTAPI允许为它生成的所有响应添加一个默认头,那就太好了。你的想法是什么?不要说我懒惰:)

由于Spring数据REST构建在Spring MVC之上,最简单的方法是配置自定义
HandlerInterceptor
,如中所述


使用Spring数据REST,最简单的方法是扩展
RepositoryRestMVCCConfiguration
并覆盖
repositoryExporterHandlerMapping
,调用父方法,然后在其上调用
…setInterceptors(…)

用于寻找实际实现细节的人

拦截器

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
public class RepositoryConfig extends
        RepositoryRestMvcConfiguration {

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

        mapping.setInterceptors(new Object[] { new CustomInterceptor() });
        return mapping;
    }
}

最后,我设法使定制拦截器的设置也在SpringDataREST2.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);  
    }

}
@配置
公共类RestMvcConfig扩展了RepositoryRestMvcConfiguration{
@自动连线用户拦截器用户拦截器;
@自动连线应用上下文应用上下文;
@凌驾
公共DelegatingHandlerMapping restHandlerMapping(){
RepositoryRestHandlerMapping repositoryMapping=新的RepositoryRestHandlerMapping(resourceMappings(),config());
setInterceptors(新对象[]{userInterceptor});//修复:定义拦截器的方法不好
setJpaHelper(jpaHelper());
setApplicationContext(applicationContext);
repositoryMapping.AfterPropertieSet();
BasePathAwareHandlerMapping basePathMapping=新的BasePathAwareHandlerMapping(config());
setApplicationContext(applicationContext);
basePathMapping.AfterPropertieSet();
列表映射=新建ArrayList();
add(basePathMapping);
add(repositoryMapping);
返回新的DelegatingHandlerMapping(映射);
}
}
我必须重写
restHandlerMapping
方法,复制粘贴它的内容并添加一行
repositoryMapping.setInterceptors
来添加自定义拦截器,在我的例子中是
UserInterceptor


有更好的方法吗?

是的,这对我来说也很有效,但在新版本中,
repositoryExporterHandlerMapping
方法已经消失,试图以类似的方式调整
reshandlermapping
也不起作用。使用
addInterceptors
也不起作用@Oliver_Gierke,为Spring数据REST存储库定义拦截器的新方法是什么?对于将来搜索此问题的人,可以参考哪一种更好的解决方案,我认为:)对于找到此答案的人,请稍微提醒一下:
postHandle
在ResponseEntity方法方面存在一些问题(在添加标题之前提交响应)。您应该改用ControllerAdvice。此处的详细信息:对于将来搜索此问题的人,可以参考哪种更好的解决方案,我认为:)