bug-spring swagger3.0.0 post不显示reqeust body参数

今天发现项目中swagger3.0.0版本的post方法的@ApiImplicitParam注解不生效, 经查询得知是swagger3.0.0 spring boot 启动器的bug

相关连接:

https://stackoverflow.com/questions/38776088/spring-boot-swagger-swagger-ui-and-requestbody-has-data-type-string

https://github.com/springfox/springfox/issues/1344

情况如下:

原本的接口注释为:

1
2
3
4
5
6
7
8
9
10
11
12
13
@ApiOperation("添加某tag至resident")
@ApiImplicitParams({
@ApiImplicitParam(name = "residentId", value = "居民id", dataTypeClass = Integer.class, paramType = "body", example = "44"),
@ApiImplicitParam(name = "tagId", value = "tag id", dataTypeClass = Integer.class, paramType = "body", example = "1")
})
@PostMapping("/resident")
public JsonResult<Integer> addResidentTag(@RequestParam("residentId") int residentId, @RequestParam("tagId") int tagId) {
Resident resident = residentService.getResidentById(residentId);
Tag tag = tagService.getTagById(tagId);
int addResidentTag = residentTagService.addResidentTag(new ResidentTag(tag, resident));
return new JsonResult<>(addResidentTag);
}

上传参数为residentId和tagId,都是在body中,于是指定了参数body, 发现结果为空

image-20201210113121279

尝试:

  1. 更改@RequestParam参数为@RequestBody: 不可取,因为@RequestBody参数是将上传的信息转化为对应的实体类的注解
  2. 修改paramType为form: 无效
  3. 在ApiOperation中添加consumes指定xml格式: 无效

于是决定新增dto类来使用RequestBody看看能不能解决

1
2
3
4
5
6
7
8
9
10
11
12
@ApiModel(value = "居民标签DTO模型")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResidentTagDTO {
@ApiModelProperty("映射表id")
private int id;
@ApiModelProperty("居民id")
private int residentId;
@ApiModelProperty("标签id")
private int tagId;
}

然后在Controller中修改参数为dto类,并且在该参数上加注解@ApiParam

1
2
3
4
5
6
7
8
9
@ApiOperation(
value = "添加某tag至resident",
consumes = "application/json",
notes = "添加某tag到居民,这里只是单纯的添加关联,并没有进行重复检查和存在检查")
@PostMapping("/resident")
public JsonResult<Integer> addResidentTag( @ApiParam(name = "residentTagDTO",value = "添加tag至居民") @RequestBody ResidentTagDTO residentTagDTO) {
int addResidentTag = residentTagService.addResidentTag(residentTagDTO);
return new JsonResult<>(addResidentTag);
}

这样的话结果是可以显示的

image-20201210114021440

image-20201210114038558