优化ApiArgumentResolver

1.x
tanghc 6 years ago
parent dcd5017b16
commit ff20b1069a
  1. 4
      doc/docs/files/10020_新增接口.md
  2. 5
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java
  3. 4
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java
  4. 2
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/JSR303DemoController.java
  5. 6
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/param/GoodsParam.java

@ -59,14 +59,12 @@ public class GoodsParam {
```java
@ApiMapping(value = "goods.add")
public Object addGoods(GoodsParam param/* 业务参数必须放在第一位对应biz_content */, HttpServletRequest request) {
public Object addGoods(GoodsParam param, HttpServletRequest request) {
System.out.println(request.getParameter("method"));
return param;
}
```
**注意**:业务参数必须放在第一位
## 接口命名
接口命名没有做强制要求,但我们还是推荐按照下面的方式进行命名:

@ -28,9 +28,8 @@ public class ApiArgumentResolver implements HandlerMethodArgumentResolver {
}
boolean hasAnnotation = methodParameter.getMethodAnnotation(ApiMapping.class) != null
|| methodParameter.getMethodAnnotation(ApiAbility.class) != null;
boolean isFirstParameter = methodParameter.getParameterIndex() == 0;
// 有注解,并且是第一个参数
return hasAnnotation && isFirstParameter;
// 有注解
return hasAnnotation;
}
@Override

@ -29,10 +29,10 @@ public class AlipayController {
return story;
}
@ApiOperation(value="获取故事信息", notes = "说明接口的详细信息,介绍,用途,注意事项等。")
@ApiOperation(value = "获取故事信息", notes = "说明接口的详细信息,介绍,用途,注意事项等。")
@ApiMapping(value = "alipay.story.find")
// 参数必须封装在类中
public StoryVO getStory2(StoryParam story /* 业务参数必须放在第一位对应biz_content */, HttpServletRequest request) {
public StoryVO getStory2(StoryParam story, HttpServletRequest request) {
StoryVO storyVO = new StoryVO();
storyVO.id = 1L;
storyVO.name = "白雪公主";

@ -14,7 +14,7 @@ import javax.servlet.http.HttpServletRequest;
public class JSR303DemoController {
@ApiMapping(value = "goods.add")
public Object addGoods(GoodsParam param/* 业务参数必须放在第一位对应biz_content */, HttpServletRequest request) {
public Object addGoods(GoodsParam param, HttpServletRequest request) {
System.out.println(request.getParameter("method"));
return param;
}

@ -3,14 +3,14 @@ package com.gitee.sop.storyweb.controller.param;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotBlank;
@Data
public class GoodsParam {
// @NotEmpty(message = "商品名称不能为空")
@NotBlank(message = "商品名称不能为空")
private String goods_name;
@NotEmpty(message = "{goods.remark.notNull}")
@NotBlank(message = "{goods.remark.notNull}")
private String goods_remark;
// 传参的格式:{xxx}=value1,value2...

Loading…
Cancel
Save