restful支持@PathVariable

pull/1/head
tanghc 5 years ago
parent caaff21876
commit 0cc8c0b991
  1. 3
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/SopRouteLocator.java
  2. 33
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/ZuulRouteRepository.java
  3. 2
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ApiMetaBuilder.java
  4. 11
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java
  5. 19
      sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RestServlet.java
  6. 30
      sop-gateway/src/test/java/com/gitee/sop/gateway/UrlPatternTest.java

@ -1,5 +1,6 @@
package com.gitee.sop.gatewaycommon.zuul.route;
import com.gitee.sop.gatewaycommon.bean.AbstractTargetRoute;
import com.gitee.sop.gatewaycommon.param.ApiParam;
import com.gitee.sop.gatewaycommon.zuul.ZuulContext;
import org.springframework.cloud.netflix.zuul.filters.Route;
@ -33,7 +34,7 @@ public class SopRouteLocator implements RouteLocator, Ordered {
public List<Route> getRoutes() {
return zuulRouteRepository.getAll()
.parallelStream()
.map(zuulTargetRoute -> zuulTargetRoute.getTargetRouteDefinition())
.map(AbstractTargetRoute::getTargetRouteDefinition)
.collect(Collectors.toList());
}

@ -1,6 +1,9 @@
package com.gitee.sop.gatewaycommon.zuul.route;
import com.gitee.sop.gatewaycommon.manager.RouteRepository;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import java.util.Collection;
import java.util.List;
@ -14,6 +17,9 @@ import java.util.stream.Collectors;
* @author tanghc
*/
public class ZuulRouteRepository implements RouteRepository<ZuulTargetRoute> {
private PathMatcher pathMatcher = new AntPathMatcher();
/**
* keynameVersion
*/
@ -24,7 +30,32 @@ public class ZuulRouteRepository implements RouteRepository<ZuulTargetRoute> {
if (id == null) {
return null;
}
return nameVersionTargetRouteMap.get(id);
ZuulTargetRoute zuulTargetRoute = nameVersionTargetRouteMap.get(id);
if (zuulTargetRoute != null) {
return zuulTargetRoute;
}
for (Map.Entry<String, ZuulTargetRoute> entry : nameVersionTargetRouteMap.entrySet()) {
String pattern = entry.getKey();
if (this.pathMatcher.match(pattern, id)) {
return clone(id, entry.getValue());
}
}
return null;
}
private ZuulTargetRoute clone(String path, ZuulTargetRoute zuulTargetRoute) {
Route targetRouteDefinition = zuulTargetRoute.getTargetRouteDefinition();
Route route = new Route(
targetRouteDefinition.getId()
,path
,targetRouteDefinition.getLocation()
,targetRouteDefinition.getPrefix()
,targetRouteDefinition.getRetryable()
, null
);
return new ZuulTargetRoute(zuulTargetRoute.getServiceRouteInfo()
, zuulTargetRoute.getRouteDefinition()
, route);
}
@Override

@ -95,7 +95,7 @@ public class ApiMetaBuilder {
if (path.contains("$") || isIgnorePattern(path)) {
return null;
}
ServiceApiInfo.ApiMeta apiMeta = new ServiceApiInfo.ApiMeta(path, path, "1.0");
ServiceApiInfo.ApiMeta apiMeta = new ServiceApiInfo.ApiMeta(path, path, "");
apiMeta.setIgnoreValidate(BooleanUtils.toInteger(true));
apiMeta.setMergeResult(BooleanUtils.toInteger(false));
apiMeta.setPermission(BooleanUtils.toInteger(false));

@ -2,6 +2,7 @@ package com.gitee.sop.storyweb.controller;
import com.gitee.sop.servercommon.util.UploadUtil;
import lombok.Data;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@ -14,6 +15,7 @@ import java.util.Collection;
/**
* 传统web开发实例
*
* @author tanghc
*/
@RestController
@ -72,6 +74,15 @@ public class TraditionalWebappController {
return food;
}
// http://localhost:2222/food/get/3 本地
// http://localhost:8081/rest/food/get/3 网关访问
@RequestMapping("/get/{id}")
public Food getById(@PathVariable("id") Integer id) {
Food food = new Food();
food.setId(id);
return food;
}
@Data
public static class Food {
private Integer id;

@ -19,13 +19,13 @@ import java.io.IOException;
@WebServlet(urlPatterns = "/rest/*")
public class RestServlet extends HttpServlet {
private static final String REST_PATH = "/rest";
private static final String EMPTY_VERSION = "";
@Value("${zuul.servlet-path:/zuul}")
private String path;
@Value("${zuul.rest-default-version:1.0}")
private String defaultVersion;
@Value("${sop.restful.path:/rest}")
private String restPath;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
@ -35,16 +35,11 @@ public class RestServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String url = request.getRequestURL().toString();
int index = url.indexOf(REST_PATH);
int index = url.indexOf(restPath);
// 取/rest的后面部分
String path = url.substring(index + REST_PATH.length());
String method = path;
String version = request.getParameter(ParamNames.VERSION_NAME);
if (version == null) {
version = defaultVersion;
}
request.setAttribute(SopConstants.REDIRECT_METHOD_KEY, method);
request.setAttribute(SopConstants.REDIRECT_VERSION_KEY, version);
String path = url.substring(index + restPath.length());
request.setAttribute(SopConstants.REDIRECT_METHOD_KEY, path);
request.setAttribute(SopConstants.REDIRECT_VERSION_KEY, EMPTY_VERSION);
request.getRequestDispatcher(this.path).forward(request, response);
}

@ -0,0 +1,30 @@
package com.gitee.sop.gateway;
import junit.framework.TestCase;
import org.junit.Assert;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
/**
* @author tanghc
*/
public class UrlPatternTest extends TestCase {
private PathMatcher pathMatcher = new AntPathMatcher();
public void testA() {
Assert.assertTrue(match("/food/get/{id}", "/food/get/2"));
Assert.assertTrue(match("/food/get/{id}1.0", "/food/get/21.0"));
}
/**
* @param pattern food/get/{id}
* @param lookupPath /food/get/2
*
* @return
*/
public boolean match(String pattern, String lookupPath) {
return this.pathMatcher.match(pattern, lookupPath);
}
}
Loading…
Cancel
Save