From 55ca50890571a29ccc069c150d610315d4b9c716 Mon Sep 17 00:00:00 2001 From: tanghc Date: Thu, 25 Jul 2019 13:37:26 +0800 Subject: [PATCH 1/3] 1.13.1 --- doc/docs/_sidebar.md | 60 +++++++------- doc/docs/files/10100_传统web开发.md | 83 +++++++++++++------ .../sop/gatewaycommon/util/MappingUtil.java | 21 +++++ .../sop/servercommon/bean/ServiceConfig.java | 5 ++ .../WebappServiceConfiguration.java | 1 + .../manager/DefaultRequestMappingEvent.java | 5 +- .../sop/servercommon/mapping/MappingUtil.java | 12 ++- .../TraditionalWebappController.java | 10 +-- .../sop/gateway/SopGatewayApplication.java | 3 + .../controller/RedirectController.java | 11 --- .../sop/gateway/controller/RestServlet.java | 44 ++++++++++ 11 files changed, 179 insertions(+), 76 deletions(-) create mode 100644 sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/MappingUtil.java create mode 100644 sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RestServlet.java diff --git a/doc/docs/_sidebar.md b/doc/docs/_sidebar.md index dd8bf3d4..1249b307 100644 --- a/doc/docs/_sidebar.md +++ b/doc/docs/_sidebar.md @@ -1,32 +1,32 @@ -* [首页](/?t=1563956863130) +* [首页](/?t=1564027102043) * 开发文档 - * [快速体验](files/10010_快速体验.md?t=1563956863132) - * [项目接入到SOP](files/10011_项目接入到SOP.md?t=1563956863153) - * [新增接口](files/10020_新增接口.md?t=1563956863153) - * [业务参数校验](files/10030_业务参数校验.md?t=1563956863153) - * [错误处理](files/10040_错误处理.md?t=1563956863153) - * [编写文档](files/10041_编写文档.md?t=1563956863153) - * [接口交互详解](files/10050_接口交互详解.md?t=1563956863153) - * [easyopen支持](files/10070_easyopen支持.md?t=1563956863154) - * [使用签名校验工具](files/10080_使用签名校验工具.md?t=1563956863154) - * [ISV管理](files/10085_ISV管理.md?t=1563956863154) - * [自定义路由](files/10086_自定义路由.md?t=1563956863154) - * [路由授权](files/10090_路由授权.md?t=1563956863154) - * [接口限流](files/10092_接口限流.md?t=1563956863154) - * [监控日志](files/10093_监控日志.md?t=1563956863154) - * [SDK开发](files/10095_SDK开发.md?t=1563956863154) - * [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1563956863154) - * [应用授权](files/10097_应用授权.md?t=1563956863155) - * [更改数据节点名称](files/10099_更改数据节点名称.md?t=1563956863155) - * [传统web开发](files/10100_传统web开发.md?t=1563956863155) - * [自定义过滤器](files/10102_自定义过滤器.md?t=1563956863155) - * [文件上传](files/10104_文件上传.md?t=1563956863155) - * [nacos注册中心](files/10106_nacos注册中心.md?t=1563956863155) - * [扩展其它注册中心](files/10107_扩展其它注册中心.md?t=1563956863155) - * [配置Sleuth链路追踪](files/10109_配置Sleuth链路追踪.md?t=1563956863155) + * [快速体验](files/10010_快速体验.md?t=1564027102045) + * [项目接入到SOP](files/10011_项目接入到SOP.md?t=1564027102062) + * [新增接口](files/10020_新增接口.md?t=1564027102062) + * [业务参数校验](files/10030_业务参数校验.md?t=1564027102062) + * [错误处理](files/10040_错误处理.md?t=1564027102062) + * [编写文档](files/10041_编写文档.md?t=1564027102063) + * [接口交互详解](files/10050_接口交互详解.md?t=1564027102063) + * [easyopen支持](files/10070_easyopen支持.md?t=1564027102063) + * [使用签名校验工具](files/10080_使用签名校验工具.md?t=1564027102063) + * [ISV管理](files/10085_ISV管理.md?t=1564027102063) + * [自定义路由](files/10086_自定义路由.md?t=1564027102063) + * [路由授权](files/10090_路由授权.md?t=1564027102063) + * [接口限流](files/10092_接口限流.md?t=1564027102063) + * [监控日志](files/10093_监控日志.md?t=1564027102063) + * [SDK开发](files/10095_SDK开发.md?t=1564027102063) + * [使用SpringCloudGateway](files/10096_使用SpringCloudGateway.md?t=1564027102064) + * [应用授权](files/10097_应用授权.md?t=1564027102064) + * [更改数据节点名称](files/10099_更改数据节点名称.md?t=1564027102064) + * [传统web开发](files/10100_传统web开发.md?t=1564027102064) + * [自定义过滤器](files/10102_自定义过滤器.md?t=1564027102064) + * [文件上传](files/10104_文件上传.md?t=1564027102064) + * [nacos注册中心](files/10106_nacos注册中心.md?t=1564027102064) + * [扩展其它注册中心](files/10107_扩展其它注册中心.md?t=1564027102064) + * [配置Sleuth链路追踪](files/10109_配置Sleuth链路追踪.md?t=1564027102064) * 原理分析 - * [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1563956863156) - * [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1563956863156) - * [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1563956863156) - * [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1563956863156) - * [常见问题](files/90100_常见问题.md?t=1563956863156) + * [原理分析之@ApiMapping](files/90010_原理分析之@ApiMapping.md?t=1564027102064) + * [原理分析之路由存储](files/90011_原理分析之路由存储.md?t=1564027102064) + * [原理分析之如何路由](files/90012_原理分析之如何路由.md?t=1564027102064) + * [原理分析之文档归纳](files/90013_原理分析之文档归纳.md?t=1564027102064) + * [常见问题](files/90100_常见问题.md?t=1564027102065) diff --git a/doc/docs/files/10100_传统web开发.md b/doc/docs/files/10100_传统web开发.md index e6283544..1fa3eaf8 100644 --- a/doc/docs/files/10100_传统web开发.md +++ b/doc/docs/files/10100_传统web开发.md @@ -1,5 +1,7 @@ # 传统web开发 +SOP既可以作为网关服务开发,又可以作为传统的webapp服务开发,传统web开发意思是像普通的web开发那样提供restful接口,没有签名校验功能。 + 本篇介绍如何使用SOP进行传统web服务开发,即对接前端应用(H5、小程序、App)。 - 网关ZuulConfig继承WebappZuulConfiguration类 @@ -35,6 +37,51 @@ public class OpenServiceConfig extends WebappServiceConfiguration { 其它内容不变 +- 前端app请求网关 + +请求格式为:`http://ip:port/rest/your_path`,其中`http://ip:port/rest/`为固定部分,后面跟微服务请求路径。 + +注意:为了确保各个微服务路径不冲突,必须保证类上方定义的`@RequestMapping`内容唯一,不与其它微服务重复。 + +下面是一个微服务的接口例子 + +```java +@RestController +@RequestMapping("food") +public class TraditionalWebappController { + @ApiMapping(value = "getFoodById", method = RequestMethod.GET) + public Food getFoodById(Integer id) { + Food food = new Food(); + food.setId(id); + food.setName("香蕉"); + food.setPrice(new BigDecimal(20.00)); + return food; + } + + // 加版本号 + @ApiMapping(value = "getFoodById", method = RequestMethod.GET, version = "1.1") + public Food getFoodById2(Integer id) { + Food food = new Food(); + food.setId(id); + food.setName("香蕉2"); + food.setPrice(new BigDecimal(22.00)); + return food; + } +} +``` + +这是一个`食品服务`例子,假设网关ip为10.0.1.11,端口8081;食品服务ip为10.0.1.22,端口2222 + +1. 网关访问:`http://10.0.1.11:8081/rest/food/getFoodById?id=2`。加版本号:`http://localhost:8081/rest/food/getFoodById?id=2&version=1.1` + +2. 本地访问:`http://10.0.1.22:2222/food/getFoodById/?id=2` + +更多例子,可查看源码类:`TraditionalWebappController.java` + +由此可见,对于前端调用者来说,它把网关看做一个大服务,只访问网关提供的请求,不需要关心网关后面的路由转发。网关后面各个微服务独自管理, +微服务之间的调用可以使用dubbo或feign,有了版本号的管理,可以做到服务的平滑升级,对用户来说都是无感知的。结合SOP-Admin提供的上下线功能, +可实现预发布环境功能。 + - 封装请求工具【可选】 封装请求,方便调用,针对vue的封装如下: @@ -46,24 +93,20 @@ import axios from 'axios' const client = axios.create({ baseURL: process.env.BASE_API, // api 的 base_url timeout: 5000, // 请求超时时间 - headers: { 'Content-Type': 'application/x-www-form-urlencoded' } + headers: { 'Content-Type': 'application/json' } }) const RequestUtil = { /** * 请求接口 - * @param method 接口名,如:goods.get,goods.get - * @param version 版本号,如:1.0 + * @param url 请求路径,如http://localhost:8081/rest/food/getFoodById * @param data 请求数据,json格式 * @param callback 成功回调 * @param errorCallback 失败回调 */ - post: function(method, version, data, callback, errorCallback) { - client.post(''/* 这里不用填 */, { - method: method, - version: version, - biz_content: JSON.stringify(data) - }).then(function(response) { + post: function(url, data, callback, errorCallback) { + client.post(url, data) + .then(function(response) { const resp = response.result const code = resp.code // 成功,网关正常且业务正常 @@ -94,23 +137,18 @@ jQuery版本如下: var RequestUtil = { /** * 请求接口 - * @param method 接口名,如:goods.get,goods.get - * @param version 版本号,如:1.0 + * @param url 请求路径,如http://localhost:8081/rest/food/getFoodById * @param data 请求数据,json格式 * @param callback 成功回调 * @param errorCallback 失败回调 */ - post: function(method, version, data, callback, errorCallback) { + post: function(url, data, callback, errorCallback) { $.ajax({ url: 'http://localhost:8081/api' // 网关url , type: 'post' - , headers: { 'Content-Type': 'application/x-www-form-urlencoded' } - , data: { - method: method, - version: version, - biz_content: JSON.stringify(data) - } - ,success:function(response){ + , headers: { 'Content-Type': 'application/json' } + , data: data + ,success:function(response) { var resp = response.result var code = resp.code // 成功,网关正常且业务正常 @@ -137,13 +175,8 @@ $(function () { id: 1 ,name: '葫芦娃' } - RequestUtil.post('alipay.story.get', '1.0', data, function (result) { + RequestUtil.post('http://localhost:8081/rest/food/getFoodById', data, function (result) { console.log(result) }); }) ``` - -1.7.1开始支持接口名版本号放在url后面,规则:`http://host:port/{method}/{version}/`(最后的`/`不能少),如:`http://localhost:8081/story.demo.get/1.0/` -等同于:`http://localhost:8081/api?method=story.demo.get&version=1.0`。 - -把接口名版本号放在url后面的好处是调用接口一目了然,在浏览器F12调试的时候特别有用,可以一眼看到调用了哪些接口,否则将会看到全部都是api请求,需要点开查看request header才能知道到底调用了哪个接口 diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/MappingUtil.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/MappingUtil.java new file mode 100644 index 00000000..ae2dce58 --- /dev/null +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/MappingUtil.java @@ -0,0 +1,21 @@ +package com.gitee.sop.gatewaycommon.util; + +import org.springframework.util.StringUtils; + +public class MappingUtil { + + public static final char SEPARATOR_CHAR = '/'; + + /** + * 将springmvc接口路径转换成SOP方法名 + * @param path springmvc路径,如/a/b,/goods/listGoods + * @return 返回接口方法名,/goods/listGoods -> goods.listGoods + */ + public static String buildApiName(String path) { + path = StringUtils.trimLeadingCharacter(path, SEPARATOR_CHAR); + path = StringUtils.trimTrailingCharacter(path, SEPARATOR_CHAR); + path = path.replace(SEPARATOR_CHAR, '.'); + return path; + } + +} \ No newline at end of file diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceConfig.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceConfig.java index 37bce8cb..24740eed 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceConfig.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ServiceConfig.java @@ -70,4 +70,9 @@ public class ServiceConfig { */ private boolean permission; + /** + * 是否传统web开发模式 + */ + private boolean webappMode; + } diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/WebappServiceConfiguration.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/WebappServiceConfiguration.java index eac13717..7e8054d9 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/WebappServiceConfiguration.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/WebappServiceConfiguration.java @@ -10,5 +10,6 @@ public class WebappServiceConfiguration extends BaseServiceConfiguration { static { // 默认版本号为1.0 ServiceConfig.getInstance().setDefaultVersion("1.0"); + ServiceConfig.getInstance().setWebappMode(true); } } diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/DefaultRequestMappingEvent.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/DefaultRequestMappingEvent.java index 214af30c..e633b551 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/DefaultRequestMappingEvent.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/DefaultRequestMappingEvent.java @@ -1,6 +1,7 @@ package com.gitee.sop.servercommon.manager; import com.gitee.sop.servercommon.bean.ServiceApiInfo; +import com.gitee.sop.servercommon.bean.ServiceConfig; import com.gitee.sop.servercommon.mapping.ApiMappingInfo; import com.gitee.sop.servercommon.mapping.ApiMappingRequestCondition; import com.gitee.sop.servercommon.mapping.MappingUtil; @@ -83,9 +84,10 @@ public class DefaultRequestMappingEvent implements RequestMappingEvent { ApiMappingInfo apiMappingInfo = condition.getApiMappingInfo(); String name = apiMappingInfo.getName(); String version = apiMappingInfo.getVersion(); + // 方法完整的path,如: /goods/listGoods,/users/user/get String path = patterns.iterator().next(); // 不是ApiMapping注解的接口,name属性是null - if (name == null) { + if (name == null || ServiceConfig.getInstance().isWebappMode()) { name = buildName(path); } this.checkApiName(name); @@ -104,6 +106,7 @@ public class DefaultRequestMappingEvent implements RequestMappingEvent { } } + protected String buildName(String path) { return MappingUtil.buildApiName(path); } diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/MappingUtil.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/MappingUtil.java index 4b3b1712..0b4bf248 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/MappingUtil.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/MappingUtil.java @@ -6,15 +6,19 @@ import org.springframework.util.StringUtils; * @author tanghc */ public class MappingUtil { + + public static final char SEPARATOR_CHAR = '/'; + /** * 将springmvc接口路径转换成SOP方法名 * @param path springmvc路径,如/a/b,/goods/listGoods - * @return + * @return 返回接口方法名,/goods/listGoods -> goods.listGoods */ public static String buildApiName(String path) { - path = StringUtils.trimLeadingCharacter(path, '/'); - path = StringUtils.trimTrailingCharacter(path, '/'); - path = path.replace("/", "."); + path = StringUtils.trimLeadingCharacter(path, SEPARATOR_CHAR); + path = StringUtils.trimTrailingCharacter(path, SEPARATOR_CHAR); + path = path.replace(SEPARATOR_CHAR, '.'); return path; } + } diff --git a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java index d36820c1..08ac2066 100644 --- a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java +++ b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java @@ -22,7 +22,7 @@ import java.util.Collection; public class TraditionalWebappController { - // http://localhost:8081/getFoodById?id=1 网关入口 + // http://localhost:8081/rest/food/getFoodById?id=1 网关入口 // http://localhost:2222/food/getFoodById/?id=12 本地入口 @ApiMapping(value = "getFoodById", method = RequestMethod.GET) public Food getFoodById(Integer id) { @@ -33,7 +33,7 @@ public class TraditionalWebappController { return food; } - // http://localhost:8081/getFoodById?id=2&version=1.1 网关入口 + // http://localhost:8081/rest/food/getFoodById?id=2&version=1.1 网关入口 // http://localhost:2222/food/getFoodById/?id=12&version=1.1 @ApiMapping(value = "getFoodById", method = RequestMethod.GET, version = "1.1") public Food getFoodById2(Integer id) { @@ -44,13 +44,13 @@ public class TraditionalWebappController { return food; } - // http://localhost:8081/getFoodByObj?id=2 - @ApiMapping(value = "getFoodByObj", method = RequestMethod.GET) + // http://localhost:8081/rest/food/get/getFoodByObj?id=2 + @ApiMapping(value = "/get/getFoodByObj", method = RequestMethod.GET) public Food getFoodByObj(Food food) { return food; } - // http://localhost:8081/saveFood + // http://localhost:8081/rest/food/saveFood @ApiMapping(value = "saveFood", method = RequestMethod.POST) public Food saveFood(@RequestBody Food food) { food.setId(3); diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/SopGatewayApplication.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/SopGatewayApplication.java index cc7af2ed..8b3fe901 100644 --- a/sop-gateway/src/main/java/com/gitee/sop/gateway/SopGatewayApplication.java +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/SopGatewayApplication.java @@ -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 { diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RedirectController.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RedirectController.java index a2bc0db2..a1d09f27 100644 --- a/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RedirectController.java +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RedirectController.java @@ -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); - } - } diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RestServlet.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RestServlet.java new file mode 100644 index 00000000..ef9a3888 --- /dev/null +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RestServlet.java @@ -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); + } + +} \ No newline at end of file From eb4612f22f06485e14131ecfb16b5e4ff340dd35 Mon Sep 17 00:00:00 2001 From: tanghc Date: Thu, 25 Jul 2019 13:51:03 +0800 Subject: [PATCH 2/3] 1.13.1 --- .../sop/gatewaycommon/util/MappingUtil.java | 21 ---------------- .../sop/gatewaycommon/util/RouteUtil.java | 15 ++++++++++++ .../manager/DefaultRequestMappingEvent.java | 4 ++-- .../sop/servercommon/mapping/MappingUtil.java | 24 ------------------- .../sop/servercommon/mapping/RouteUtil.java | 23 ++++++++++++++++++ .../DocumentationPluginsManagerExt.java | 4 ++-- .../sop/gateway/controller/RestServlet.java | 11 +++++---- .../main/resources/application-dev.properties | 2 ++ 8 files changed, 51 insertions(+), 53 deletions(-) delete mode 100644 sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/MappingUtil.java delete mode 100644 sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/MappingUtil.java create mode 100644 sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/RouteUtil.java diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/MappingUtil.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/MappingUtil.java deleted file mode 100644 index ae2dce58..00000000 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/MappingUtil.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.gitee.sop.gatewaycommon.util; - -import org.springframework.util.StringUtils; - -public class MappingUtil { - - public static final char SEPARATOR_CHAR = '/'; - - /** - * 将springmvc接口路径转换成SOP方法名 - * @param path springmvc路径,如/a/b,/goods/listGoods - * @return 返回接口方法名,/goods/listGoods -> goods.listGoods - */ - public static String buildApiName(String path) { - path = StringUtils.trimLeadingCharacter(path, SEPARATOR_CHAR); - path = StringUtils.trimTrailingCharacter(path, SEPARATOR_CHAR); - path = path.replace(SEPARATOR_CHAR, '.'); - return path; - } - -} \ No newline at end of file diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/RouteUtil.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/RouteUtil.java index 773e1486..86f0248a 100644 --- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/RouteUtil.java +++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/util/RouteUtil.java @@ -1,5 +1,7 @@ package com.gitee.sop.gatewaycommon.util; +import org.springframework.util.StringUtils; + /** * @author tanghc */ @@ -22,6 +24,19 @@ public class RouteUtil { } } + /** + * 将springmvc接口路径转换成SOP方法名 + * + * @param path springmvc路径,如:/goods/listGoods + * @return 返回接口方法名,/goods/listGoods -> goods.listGoods + */ + public static String buildApiName(String path) { + char separatorChar = '/'; + path = StringUtils.trimLeadingCharacter(path, separatorChar); + path = StringUtils.trimTrailingCharacter(path, separatorChar); + return path.replace(separatorChar, '.'); + } + public static String getZuulLocation(String uri) { if (uri.toLowerCase().startsWith(PROTOCOL_LOAD_BALANCE)) { return uri.substring(PROTOCOL_LOAD_BALANCE.length()); diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/DefaultRequestMappingEvent.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/DefaultRequestMappingEvent.java index e633b551..4b3474db 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/DefaultRequestMappingEvent.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/DefaultRequestMappingEvent.java @@ -4,7 +4,7 @@ import com.gitee.sop.servercommon.bean.ServiceApiInfo; import com.gitee.sop.servercommon.bean.ServiceConfig; import com.gitee.sop.servercommon.mapping.ApiMappingInfo; import com.gitee.sop.servercommon.mapping.ApiMappingRequestCondition; -import com.gitee.sop.servercommon.mapping.MappingUtil; +import com.gitee.sop.servercommon.mapping.RouteUtil; import lombok.Getter; import org.apache.commons.lang3.BooleanUtils; import org.springframework.core.env.Environment; @@ -108,7 +108,7 @@ public class DefaultRequestMappingEvent implements RequestMappingEvent { protected String buildName(String path) { - return MappingUtil.buildApiName(path); + return RouteUtil.buildApiName(path); } } diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/MappingUtil.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/MappingUtil.java deleted file mode 100644 index 0b4bf248..00000000 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/MappingUtil.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.gitee.sop.servercommon.mapping; - -import org.springframework.util.StringUtils; - -/** - * @author tanghc - */ -public class MappingUtil { - - public static final char SEPARATOR_CHAR = '/'; - - /** - * 将springmvc接口路径转换成SOP方法名 - * @param path springmvc路径,如/a/b,/goods/listGoods - * @return 返回接口方法名,/goods/listGoods -> goods.listGoods - */ - public static String buildApiName(String path) { - path = StringUtils.trimLeadingCharacter(path, SEPARATOR_CHAR); - path = StringUtils.trimTrailingCharacter(path, SEPARATOR_CHAR); - path = path.replace(SEPARATOR_CHAR, '.'); - return path; - } - -} diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/RouteUtil.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/RouteUtil.java new file mode 100644 index 00000000..d83f55c8 --- /dev/null +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/RouteUtil.java @@ -0,0 +1,23 @@ +package com.gitee.sop.servercommon.mapping; + +import org.springframework.util.StringUtils; + +/** + * @author tanghc + */ +public class RouteUtil { + + /** + * 将springmvc接口路径转换成SOP方法名 + * + * @param path springmvc路径,如:/goods/listGoods + * @return 返回接口方法名,/goods/listGoods -> goods.listGoods + */ + public static String buildApiName(String path) { + char separatorChar = '/'; + path = StringUtils.trimLeadingCharacter(path, separatorChar); + path = StringUtils.trimTrailingCharacter(path, separatorChar); + return path.replace(separatorChar, '.'); + } + +} diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/swagger/DocumentationPluginsManagerExt.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/swagger/DocumentationPluginsManagerExt.java index 2cdbd0a0..ca179daf 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/swagger/DocumentationPluginsManagerExt.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/swagger/DocumentationPluginsManagerExt.java @@ -3,7 +3,7 @@ package com.gitee.sop.servercommon.swagger; import com.gitee.sop.servercommon.annotation.ApiAbility; import com.gitee.sop.servercommon.annotation.ApiMapping; import com.gitee.sop.servercommon.bean.ServiceConfig; -import com.gitee.sop.servercommon.mapping.MappingUtil; +import com.gitee.sop.servercommon.mapping.RouteUtil; import com.google.common.base.Optional; import springfox.documentation.service.Operation; import springfox.documentation.service.StringVendorExtension; @@ -42,7 +42,7 @@ public class DocumentationPluginsManagerExt extends DocumentationPluginsManager if (abilityOptional.isPresent()) { ApiAbility apiAbility = abilityOptional.get(); String mappingPattern = operationContext.requestMappingPattern(); - String name = MappingUtil.buildApiName(mappingPattern); + String name = RouteUtil.buildApiName(mappingPattern); String version = buildVersion(apiAbility.version()); vendorExtensions.add(new StringVendorExtension(SOP_NAME, name)); vendorExtensions.add(new StringVendorExtension(SOP_VERSION, version)); diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RestServlet.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RestServlet.java index ef9a3888..14becac5 100644 --- a/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RestServlet.java +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RestServlet.java @@ -2,7 +2,7 @@ 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 com.gitee.sop.gatewaycommon.util.RouteUtil; import org.springframework.beans.factory.annotation.Value; import javax.servlet.ServletException; @@ -17,9 +17,12 @@ public class RestServlet extends HttpServlet { private static final String REST_PATH = "/rest"; - @Value("${zuul.servlet-path}") + @Value("${zuul.servlet-path:/zuul}") private String path; + @Value("${zuul.rest-default-version:1.0}") + private String defaultVersion; + @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); @@ -31,10 +34,10 @@ public class RestServlet extends HttpServlet { int index = url.indexOf(REST_PATH); // 取/rest的后面部分 String path = url.substring(index + REST_PATH.length()); - String method = MappingUtil.buildApiName(path); + String method = RouteUtil.buildApiName(path); String version = request.getParameter(ParamNames.VERSION_NAME); if (version == null) { - version = "1.0"; + version = defaultVersion; } request.setAttribute(SopConstants.REDIRECT_METHOD_KEY, method); request.setAttribute(SopConstants.REDIRECT_VERSION_KEY, version); diff --git a/sop-gateway/src/main/resources/application-dev.properties b/sop-gateway/src/main/resources/application-dev.properties index 70cbea85..8517f0ee 100644 --- a/sop-gateway/src/main/resources/application-dev.properties +++ b/sop-gateway/src/main/resources/application-dev.properties @@ -21,6 +21,8 @@ zuul.FormBodyWrapperFilter.pre.disable=true zuul.Servlet30WrapperFilter.pre.disable=true # 不用改,如果要改,请全局替换修改 zuul.secret=MZZOUSTua6LzApIWXCwEgbBmxSzpzC +# 不用改 +zuul.rest-default-version=1.0 # https://blog.csdn.net/qq_36872046/article/details/81058045 # 路由转发超时时间,毫秒,默认值1000,详见:RibbonClientConfiguration.DEFAULT_READ_TIMEOUT。 From bf49c68286682da4546449f06fe3bc8e841334c9 Mon Sep 17 00:00:00 2001 From: tanghc Date: Thu, 25 Jul 2019 14:05:31 +0800 Subject: [PATCH 3/3] 1.13.1 --- changelog.md | 1 + .../sop/storyweb/controller/TraditionalWebappController.java | 4 ++-- .../src/main/java/com/gitee/sop/sdk/client/OpenRequest.java | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index bb3892eb..681d86b1 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,7 @@ ## 1.13.1 - 支持json方式请求(application/json) +- 支持传统web服务开发(见文档`传统web开发`) ## 1.13.0 diff --git a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java index 08ac2066..a2ede01a 100644 --- a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java +++ b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java @@ -44,8 +44,8 @@ public class TraditionalWebappController { return food; } - // http://localhost:8081/rest/food/get/getFoodByObj?id=2 - @ApiMapping(value = "/get/getFoodByObj", method = RequestMethod.GET) + // http://localhost:8081/rest/food/getFoodByObj?id=2 + @ApiMapping(value = "ggetFoodByObj", method = RequestMethod.GET) public Food getFoodByObj(Food food) { return food; } diff --git a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java index 8c4fe67b..4ce3b2b3 100644 --- a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java +++ b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java @@ -42,7 +42,7 @@ public class OpenRequest { } return openHttp.get(url, header); } else { - return openHttp.requestJson(url, JSON.toJSONString(form), header); + return openHttp.request(url, form, header, requestMethod.name()); } } } catch (IOException e) {