zuul怎么根據請求方式?
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;
}
}