mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
优化ApiArgumentResolver
This commit is contained in:
@@ -32,6 +32,43 @@ value就是接口名,对应客户端的`method`参数
|
|||||||
|
|
||||||
- 重启story服务,这样接口就可以使用了。
|
- 重启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;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**注意**:业务参数必须放在第一位
|
||||||
|
|
||||||
## 接口命名
|
## 接口命名
|
||||||
|
|
||||||
接口命名没有做强制要求,但我们还是推荐按照下面的方式进行命名:
|
接口命名没有做强制要求,但我们还是推荐按照下面的方式进行命名:
|
||||||
|
@@ -1,6 +1,8 @@
|
|||||||
package com.gitee.sop.servercommon.param;
|
package com.gitee.sop.servercommon.param;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
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 com.gitee.sop.servercommon.bean.ParamNames;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
@@ -21,7 +23,14 @@ public class ApiArgumentResolver implements HandlerMethodArgumentResolver {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean supportsParameter(MethodParameter methodParameter) {
|
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
|
@Override
|
||||||
|
@@ -1,14 +1,15 @@
|
|||||||
package com.gitee.sop.storyweb.controller;
|
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.servercommon.annotation.ApiMapping;
|
||||||
import com.gitee.sop.story.api.domain.Story;
|
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.Api;
|
||||||
import io.swagger.annotations.ApiModelProperty;
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -31,7 +32,7 @@ public class AlipayController {
|
|||||||
@ApiOperation(value="获取故事信息", notes = "说明接口的详细信息,介绍,用途,注意事项等。")
|
@ApiOperation(value="获取故事信息", notes = "说明接口的详细信息,介绍,用途,注意事项等。")
|
||||||
@ApiMapping(value = "alipay.story.find")
|
@ApiMapping(value = "alipay.story.find")
|
||||||
// 参数必须封装在类中
|
// 参数必须封装在类中
|
||||||
public StoryVO getStory2(StoryParam story) {
|
public StoryVO getStory2(StoryParam story /* 业务参数必须放在第一位对应biz_content */, HttpServletRequest request) {
|
||||||
StoryVO storyVO = new StoryVO();
|
StoryVO storyVO = new StoryVO();
|
||||||
storyVO.id = 1L;
|
storyVO.id = 1L;
|
||||||
storyVO.name = "白雪公主";
|
storyVO.name = "白雪公主";
|
||||||
|
@@ -4,6 +4,8 @@ import com.gitee.sop.storyweb.controller.param.GoodsParam;
|
|||||||
import com.gitee.sop.servercommon.annotation.ApiMapping;
|
import com.gitee.sop.servercommon.annotation.ApiMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 演示参数验证
|
* 演示参数验证
|
||||||
* @author tanghc
|
* @author tanghc
|
||||||
@@ -12,7 +14,8 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
public class JSR303DemoController {
|
public class JSR303DemoController {
|
||||||
|
|
||||||
@ApiMapping(value = "goods.add")
|
@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;
|
return param;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -97,6 +97,15 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<!-- 打包时跳过测试 -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>2.12.4</version>
|
||||||
|
<configuration>
|
||||||
|
<skipTests>true</skipTests>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user