优化ApiArgumentResolver

This commit is contained in:
tanghc
2019-04-29 19:58:30 +08:00
parent dcd5017b16
commit ff20b1069a
5 changed files with 9 additions and 12 deletions

View File

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

View File

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

View File

@@ -32,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 /* 业务参数必须放在第一位对应biz_content */, HttpServletRequest request) { public StoryVO getStory2(StoryParam story, HttpServletRequest request) {
StoryVO storyVO = new StoryVO(); StoryVO storyVO = new StoryVO();
storyVO.id = 1L; storyVO.id = 1L;
storyVO.name = "白雪公主"; storyVO.name = "白雪公主";

View File

@@ -14,7 +14,7 @@ import javax.servlet.http.HttpServletRequest;
public class JSR303DemoController { public class JSR303DemoController {
@ApiMapping(value = "goods.add") @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")); System.out.println(request.getParameter("method"));
return param; return param;
} }

View File

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