6

I am using Java, Spring and Jersey.

I have a REST API A which receives a query parameter called q and sends this to another API B to get a response.

I need to implement a change where I first need to pass the query from the client to another API C and use the response from API C to create a new request, i.e. basically wrap the request in an HttpServletRequestWrapper which has the new q parameter.

Hopefully this image will clarify this a bit:

enter image description here

So the idea is to use a javax.servlet.Filter for this scenario. Few questions that bug me:

  1. Is it good practice to call another API from a Filter?

  2. I am using Spring. Would it be an issue to use @Autowired for the client of API C within my Filter?

5
  • 5
    Why might it be a bad practice? Does it satisfy your requirements in a way that is maintainable, secure and performant? Mar 16, 2017 at 16:13
  • Because I could not find any example where a Filter is used to make calls to another API. And the documentation mention a few basic usages of Filters but nothing like making API2API calls from within a Filter. So I thought this might not be good practice. Any thoughts on my second question? Thanks.
    – Diyarbakir
    Mar 16, 2017 at 19:12
  • I would not use filters in this way and I would not modify (never) the incoming request and its parameters. Have you considered AOP? You could set an Advice around the Controllers and make here the changes of the inputs. Spring have two different approaches for AOP. By proxies and by AspectJ.
    – Laiv
    Mar 16, 2017 at 22:13
  • @Laiv What is your objection for using Filters for my purpose? Could you elaborate a bit more. Thanks.
    – Diyarbakir
    Mar 17, 2017 at 11:29
  • Agree with @Laiv, it seems unlikely modifying / replacing the client request in a filter would be maintainable. I'd look to implement this requirement nearer to existing business logic (if possible)
    – James
    Mar 19, 2017 at 22:36

2 Answers 2

6
+25

You are doing what is commonly known as Web Service Orchestration. See this related post on StackOverflow about orchestrating micro services.

Using a servlet filter to make a call to an external service is not recommended. According to the Oracle documentation, Filters are supposed to be used to transform the HTTP request or response or both. In this case it is not purely transforming, it is calling an external service which is not under your control, so you need to treat it as a potential failure point.

Filters should be used for transforming formats or logging etc. Put any any external calls should be done in as part of your service orchestration logic.

A limitation of orchestration logic in Filters is that the order in which it called will matter and you will need to be careful to get this order correct. From a code design perspective this make your code more fragile.

Also if you want to later do more complex orchestration of the services with timeouts etc., using filters may be problematic as you may effect other filters in the chain.

Recommended approaches would be use something like Apache Camel or Spring Integration or implement the API Gateway pattern as the above Stack Overflow link suggested.

1

Generally yes it is not good practice because usually we do not need to call external services for each endpoint and orchestration order changes for different use cases, whereas filter is used for common logic around endpoints.

But look what spring OAuth2ClientAuthenticationProcessingFilter does, it calls authorization server to fetch tokens.

So it boils down to your requirements, and if you are sure about the order of the external call and you have taken care of service resilience, then there shouldn't be any harm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.