仅使用Spring Data REST的REST API的自定义默认标头 [英] Custom default headers for REST API only using Spring Data REST

查看:78
本文介绍了仅使用Spring Data REST的REST API的自定义默认标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用例,其中我的应用程序托管REST API和Web应用程序,我们只需要向REST API添加自定义标头. REST API通过Spring Data REST启用.通常,我们可以使用Servlet Filter来实现此目的,但是我们需要编写将请求隔离到REST API的逻辑,并添加自定义标头.如果Spring Data REST API允许向其生成的所有响应中添加默认标头,那就太好了.你觉得呢?你有没有什么想法?不要说我很懒:)

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 :)

推荐答案

由于Spring Data REST是在Spring MVC之上构建的,所以最简单的方法是按照

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.

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

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

这篇关于仅使用Spring Data REST的REST API的自定义默认标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆