What's the most immediate way to just proxy a http request via a filter/interceptor?

Viewed 113

For my project I need to implement a simple proxy in spring boot by using either a filter or an interceptor. So, my microservice A will just receive a http request meant for microservice B, will forward it with all headers/query params, and return the response either good or bad with the proper response code and any response header microservice B will add.

I know there are ready to use libraries like spring-cloud-gateway or spring-cloud-zuul but they both conflict with other specifications I have and cannot use them. Moreover I don't really need any filtering/aggregation logic, I just want to make a simple proxy.

I tried doing the following:

@Component
@Order(1)
public class ByPassFilter extends OncePerRequestFilter {

      @Value("${proxy.active}")
      private Boolean active;

      @Override
      public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) {
        if(active){
             RestTemplate rest = new RestTemplate(); //overkill here?
             HttpMethod method = HttpMethod.resolve(request.getMethod());

             switch(method){
                 case GET:
                    //use rest template to do get

              }
        }else{
          filterChain.doFilter(request, response);
        }
}

But RestTemplate seems very complicated as it is supposed to be used when I want to handle/process the request, it will even return exception if the code is not 200. I also have no idea on how to write the output directly to the HttpServletResponse.

So, is there any other way even more transparent? I saw people using apache's OkHttpClient, is that a little more generic than RestTemplate?

0 Answers
Related