首頁>Club>
10
回覆列表
  • 1 # IT薺薺菜

    1、可以考慮一下繼承抽象過濾器的方式:com.netflix.zuul.ZuulFilter。關鍵程式碼如下圖所示:

    2、完整程式碼如下:

    package cn.jijicai.springboot;

    import com.netflix.zuul.ZuulFilter;

    import com.netflix.zuul.context.RequestContext;

    import org.springframework.http.HttpMethod;

    import org.springframework.stereotype.Component;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

    import java.io.IOException;

    @Component

    public class RouteFilter extends ZuulFilter {

    /**

    * 過濾器的邏輯

    */

    @Override

    public Object run() {

    //獲取當前請求上下文

    RequestContext ctx = RequestContext.getCurrentContext();

    //取出當前請求

    HttpServletRequest request = ctx.getRequest();

    HttpServletResponse response = ctx.getResponse();

    try {

    if (HttpMethod.GET.name().equals(request.getMethod())) {// GET請求

    response.sendRedirect("/path1");

    } else if (HttpMethod.POST.name().equals(request.getMethod())) {// POST請求

    response.sendRedirect("/path2");

    }

    } catch (IOException e) {

    e.printStackTrace();

    }

    return null;

    }

    @Override

    public boolean shouldFilter() {

    return false;

    }

    @Override

    public String filterType() {

    return null;

    }

    @Override

    public int filterOrder() {

    return 0;

    }

    }

  • 中秋節和大豐收的關聯?
  • 相敬如賓,相濡以沫.拼音怎麼讀?