From 3b25d73174e8d1ec54bd2e9372e56ab555773020 Mon Sep 17 00:00:00 2001 From: tanghc Date: Mon, 29 Apr 2019 19:45:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96ApiArgumentResolver?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/docs/files/10020_新增接口.md | 37 +++++++++++++++++++ .../param/ApiArgumentResolver.java | 11 +++++- .../storyweb/controller/AlipayController.java | 5 ++- .../controller/JSR303DemoController.java | 5 ++- sop-gateway/pom.xml | 9 +++++ 5 files changed, 63 insertions(+), 4 deletions(-) diff --git a/doc/docs/files/10020_新增接口.md b/doc/docs/files/10020_新增接口.md index 7571bfd5..9a0d6494 100644 --- a/doc/docs/files/10020_新增接口.md +++ b/doc/docs/files/10020_新增接口.md @@ -32,6 +32,43 @@ value就是接口名,对应客户端的`method`参数 - 重启story服务,这样接口就可以使用了。 +## 绑定业务参数 + +网关校验通过后,请求参数会传递到微服务上来,完整的参数如下所示: + +``` +请求参数:charset=utf-8&biz_content={"goods_remark":"iphone6"}&method=goods.add&format=json&app_id=2019032617262200001&sign_type=RSA2&version=1.0×tamp=2019-04-29 19:18:38 +``` + +其中biz_content部分是我们想要的,在方法上申明一个对象,对应biz_content中的内容即可完成参数绑定,并且对参数进行JSR-303校验。 + +```java +@ApiMapping(value = "goods.add") +public Object addGoods(GoodsParam param) { + return param; +} + +@Data +public class GoodsParam { + + @NotEmpty(message = "不能为空") // 支持JSR-303校验 + private String goods_remark; + +} +``` + +一般情况下,只需要获取业务参数即可,如果想要获取更多的参数,可在后面跟一个`HttpServletRequest`对象。 + +```java +@ApiMapping(value = "goods.add") +public Object addGoods(GoodsParam param/* 业务参数必须放在第一位对应biz_content */, HttpServletRequest request) { + System.out.println(request.getParameter("method")); + return param; +} +``` + +**注意**:业务参数必须放在第一位 + ## 接口命名 接口命名没有做强制要求,但我们还是推荐按照下面的方式进行命名: diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java index 0ffb944c..dbe4604c 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java @@ -1,6 +1,8 @@ package com.gitee.sop.servercommon.param; import com.alibaba.fastjson.JSON; +import com.gitee.sop.servercommon.annotation.ApiAbility; +import com.gitee.sop.servercommon.annotation.ApiMapping; import com.gitee.sop.servercommon.bean.ParamNames; import lombok.Data; import org.springframework.core.MethodParameter; @@ -21,7 +23,14 @@ public class ApiArgumentResolver implements HandlerMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter methodParameter) { - return methodParameter != null; + if (methodParameter == null) { + return false; + } + boolean hasAnnotation = methodParameter.getMethodAnnotation(ApiMapping.class) != null + || methodParameter.getMethodAnnotation(ApiAbility.class) != null; + boolean isFirstParameter = methodParameter.getParameterIndex() == 0; + // 有注解,并且是第一个参数 + return hasAnnotation && isFirstParameter; } @Override diff --git a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java index eec120fc..fd145c50 100644 --- a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java +++ b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java @@ -1,14 +1,15 @@ package com.gitee.sop.storyweb.controller; -import com.gitee.sop.storyweb.controller.param.StoryParam; import com.gitee.sop.servercommon.annotation.ApiMapping; import com.gitee.sop.story.api.domain.Story; +import com.gitee.sop.storyweb.controller.param.StoryParam; import io.swagger.annotations.Api; import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiOperation; import lombok.Data; import org.springframework.web.bind.annotation.RestController; +import javax.servlet.http.HttpServletRequest; import java.util.Date; /** @@ -31,7 +32,7 @@ public class AlipayController { @ApiOperation(value="获取故事信息", notes = "说明接口的详细信息,介绍,用途,注意事项等。") @ApiMapping(value = "alipay.story.find") // 参数必须封装在类中 - public StoryVO getStory2(StoryParam story) { + public StoryVO getStory2(StoryParam story /* 业务参数必须放在第一位对应biz_content */, HttpServletRequest request) { StoryVO storyVO = new StoryVO(); storyVO.id = 1L; storyVO.name = "白雪公主"; diff --git a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/JSR303DemoController.java b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/JSR303DemoController.java index 35831ddd..d033d601 100644 --- a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/JSR303DemoController.java +++ b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/JSR303DemoController.java @@ -4,6 +4,8 @@ import com.gitee.sop.storyweb.controller.param.GoodsParam; import com.gitee.sop.servercommon.annotation.ApiMapping; import org.springframework.web.bind.annotation.RestController; +import javax.servlet.http.HttpServletRequest; + /** * 演示参数验证 * @author tanghc @@ -12,7 +14,8 @@ import org.springframework.web.bind.annotation.RestController; public class JSR303DemoController { @ApiMapping(value = "goods.add") - public Object addGoods(GoodsParam param) { + public Object addGoods(GoodsParam param/* 业务参数必须放在第一位对应biz_content */, HttpServletRequest request) { + System.out.println(request.getParameter("method")); return param; } } diff --git a/sop-gateway/pom.xml b/sop-gateway/pom.xml index fc932851..6bf1905c 100644 --- a/sop-gateway/pom.xml +++ b/sop-gateway/pom.xml @@ -97,6 +97,15 @@ org.springframework.boot spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-surefire-plugin + 2.12.4 + + true + +