Spring cloud zuul Method for modifying the request url

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.