Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

One way is to change all the hundreds or even thousands of REST invocations to add this mapping before the REST call is initiated.

But, I was wondering if there is a smarter way of doing this using aspects/filters/listeners/interceptors in 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.

Any suggestions about solving or any other good practices are welcome.

Thank you.

like image 362
Sunil Avatar asked Aug 24 '26 09:08

Sunil


1 Answers

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. The method that needs to be overridden is where you can catch the request headers and modify them.

like image 90
Sunil Avatar answered Aug 26 '26 21:08

Sunil