Home How do we listen to any URL/end point which is not marked with @RestController annotation in Spring?
Reply: 2

How do we listen to any URL/end point which is not marked with @RestController annotation in Spring?

user3679686
1#
user3679686 Published in 2018-01-12 23:21:15Z

Is there a way that I can listen to any particular request without that request being present in any of the request mappings in Spring ? I am trying to build a gateway functionality where i need to redirect a request to a downstream API by inspecting the request URI. I would need to see if I have the downstream/internal api for that particular request and forward to the downstream API if i have that mapping.

I currently have all the downstream mapping in the database.

For example:

Client facing uri: http://external_host/api/xyz/

downstream uri: http://downstream_host/api/abc

I store this mapping of client facing uri and downstream uri in the database. So I just to want to listen to all the requests that are hitting my service and then examine on my own if I can forward the request or not based on the mapping I have stored.

All I could think was to have a controller that would accept any request but I do not like this approach as this seems pretty naive.

@RestController
@RequestMapping(value = "*")
public class TestController {

    @RequestMapping(value = "/*", method = RequestMethod.GET)
    public void GetRequestUri() {
        System.out.println("test");
    }
}

I hope my question was elaborate enough.

Chris Sekas
2#
Chris Sekas Reply to 2018-01-13 00:16:44Z

You could use Netflix Zuul which is a proxy that is doing routing and filtering, but with a different way.

If you want to build your own proxy - discovery service you need to use only one endpoint, so your implementation has nothing wrong!

Eleazar Enrique
3#
Eleazar Enrique Reply to 2018-01-13 00:55:03Z

Approach - Creating a filter

Reference: How to register a servlet filter in Spring MVC

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class RootFilter implements Filter {
    @Override
    public void destroy() {
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request,
                         ServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {
            // Evaluate the request path
            chain.doFilter(request, response);
        } catch (Exception ex) {
            //handle Exceptions.
        }
    }
}

web.xml

<filter>
    <filter-name>RootFilter</filter-name>
    <filter-class>com.....RootFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>RootFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

public class MyWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Filter[] getServletFilters() {
        return new Filter[]{new RootFilter()};
    }
}

Approach - Creating Interceptor

Reference: Spring 3 MVC Interceptor tutorial

Spring MVC provides a powerful mechanism to intercept an http request. Similar to Servlet Filter concept, Spring MVC provides a way to define special classes called Interceptors that gets called before and after a request is served.

Each interceptor you define must implement org.springframework.web.servlet.HandlerInterceptor interface. There are three methods that need to be implemented.

preHandle(..) is called before the actual handler is executed;

The preHandle(..) method returns a boolean value. You can use this method to break or continue the processing of the execution chain. When this method returns true, the handler execution chain will continue; when it returns false, the DispatcherServlet assumes the interceptor itself has taken care of requests (and, for example, rendered an appropriate view) and does not continue executing the other interceptors and the actual handler in the execution chain.

postHandle(..) is called after the handler is executed;

afterCompletion(..) is called after the complete request has finished.

These three methods should provide enough flexibility to do all kinds of preprocessing and postprocessing.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.322136 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO