This commit is contained in:
tanghc
2019-07-25 13:37:26 +08:00
parent 709186b0da
commit 55ca508905
11 changed files with 179 additions and 76 deletions

View File

@@ -2,10 +2,13 @@ package com.gitee.sop.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
// 开启网关功能
@EnableZuulProxy
// 扫描自定义的servlet类上标注@WebServle
@ServletComponentScan
@SpringBootApplication
public class SopGatewayApplication {

View File

@@ -32,15 +32,4 @@ public class RedirectController {
request.getRequestDispatcher(path).forward(request, response);
}
@RequestMapping("/{method}")
public void redirect2(
@PathVariable("method") String method
, HttpServletRequest request
, HttpServletResponse response
) throws ServletException, IOException {
request.setAttribute(SopConstants.REDIRECT_METHOD_KEY, method);
request.setAttribute(SopConstants.REDIRECT_VERSION_KEY, "1.0");
request.getRequestDispatcher(path).forward(request, response);
}
}

View File

@@ -0,0 +1,44 @@
package com.gitee.sop.gateway.controller;
import com.gitee.sop.gatewaycommon.bean.SopConstants;
import com.gitee.sop.gatewaycommon.param.ParamNames;
import com.gitee.sop.gatewaycommon.util.MappingUtil;
import org.springframework.beans.factory.annotation.Value;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/rest/*")
public class RestServlet extends HttpServlet {
private static final String REST_PATH = "/rest";
@Value("${zuul.servlet-path}")
private String path;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String url = request.getRequestURL().toString();
int index = url.indexOf(REST_PATH);
// 取/rest的后面部分
String path = url.substring(index + REST_PATH.length());
String method = MappingUtil.buildApiName(path);
String version = request.getParameter(ParamNames.VERSION_NAME);
if (version == null) {
version = "1.0";
}
request.setAttribute(SopConstants.REDIRECT_METHOD_KEY, method);
request.setAttribute(SopConstants.REDIRECT_VERSION_KEY, version);
request.getRequestDispatcher(this.path).forward(request, response);
}
}