简体   繁体   English

拦截从Java应用程序发出的所有HTTP请求

[英]Intercept all HTTP requests going out of a Java Application

In our project, we need to pass a certain header information in all the REST calls that are sent out to other internal APIs. 在我们的项目中,我们需要在发送到其他内部API的所有REST调用中传递特定的头信息。

One way is to change all the hundreds or even thousands of REST invocations to add this mapping before the REST call is initiated. 一种方法是在启动REST调用之前更改所有数百甚至数千个REST调用以添加此映射。

But, I was wondering if there is a smarter way of doing this using aspects/filters/listeners/interceptors in Spring/Java? 但是,我想知道在Spring / Java中使用方面/过滤器/监听器/拦截器是否有更聪明的方法可以做到这一点?

Example: If I make a call from REST API 1 to an endpoint, I am trying to explore a way to intercept the constructed HTTP request and add the header and fire the request. 示例:如果我从REST API 1到端点进行调用,我正在尝试探索拦截构造的HTTP请求并添加标头并触发请求的方法。

Any suggestions about solving or any other good practices are welcome. 欢迎任何有关解决或任何其他良好做法的建议。

Thank you. 谢谢。

For any person who stumbles upon this post in the future, here is what it took to make it working. 对于任何在将来偶然发现这篇文章的人来说,这就是让它发挥作用所需要的。

Implement a class that 'implements' ClientHttpRequestInterceptor. 实现一个“实现”ClientHttpRequestInterceptor的类。 The method that needs to be overridden is where you can catch the request headers and modify them. 需要重写的方法是捕获请求标头并修改它们的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM