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

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

问题描述

我有一个用例,我的应用程序托管 REST API 和 Web 应用程序,我们只需要将自定义标头添加到 REST API.REST API 是通过 Spring Data REST 启用的.通常我们可以使用 Servlet 过滤器来实现这一点,但我们需要编写将请求隔离到 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 之上,最简单的方法是配置自定义 HandlerInterceptor,如 参考文档.

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天全站免登陆