Spring cloud zuul Method for modifying the request url
Preface
In daily development, in addition to modifying the request parameters, setting the response header, and responding to the body, there is also a need for url re-modification or url modification. Here we will briefly describe how to modify the url in zuul. Let's take a look at the detailed introduction.
Forwarding Configuration
demo: ribbon: NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList listOfServers: 192.168.99.100,192.168.99.101zuul: routes: demo: path: /demo/** stripPrefix: true serviceId: demo
Filter configuration
@Componentpublic class UrlPathFilter extends ZuulFilter{ @Override public String filterType() { return FilterConstants.PRE_TYPE; } @Override public int filterOrder() { return FilterConstants.PRE_DECORATION_FILTER_ORDER + 1; } @Override public boolean shouldFilter() { final String serviceId = (String) RequestContext.getCurrentContext().get("proxy"); return "demo".equals(serviceId); } @Override public Object run() { RequestContext context = RequestContext.getCurrentContext(); Object originalRequestPath = context.get(FilterConstants.REQUEST_URI_KEY); //http://localhost:10000/demo/list/data //-->/api/prefix/list/data String modifiedRequestPath = "/api/prefix" + originalRequestPath; context.put(FilterConstants.REQUEST_URI_KEY, modifiedRequestPath); return null; }}
This is all done.