当我们访问swagger页面的时候,有时候会报类型转换的异常,但是不影响项目正常运行,有强迫症的人肯定觉得很难受!出现这种问题,是由于你的参数列表中包含数字类型的参数(Long、Integer、long、int),我们只需要给这些参数的swagger说明加上默认值即可,即给example属性设置默认值,例如:
1、参数直接在controller层的方法里面
@ApiOperation("根据id查询")
@ApiImplicitParam(name = "id", value = "用户id", required = true, example = "1")
@GetMapping("/select-by-id")
public Result<UserVO> selectById(@NotNull(message = "用户id不能为空") Long id){
return success(userService.selectById(id));
}
@ApiOperation("根据id查询")
@getMapping("/select-by-id")
public Result<UserVO> selectById(@NotNull(message = "id不能为空") @ApiParam(value = "用户id", example = "1") Long id) {
return success(userService.selectById(id));
}
@ApiOperation("用户启用/禁用接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户id", required = true, example = "1"),
@ApiImplicitParam(name = "enable", value = "启用状态", required = true)})
@PostMapping("/enable")
public Result enable(@NotNull(message = "id不能为空") Long id, @NotNull(message = "启用状态不能为空") EnableEnum enable) {
userService.updateEnable(id, enable);
return success();
}
2、参数封装在实体里,controller层通过实体接收
@Data
@ApiModel
public class CreatUserDto implements Serializable {
private static final long serialVersionUID = 3299083728881790797L;
@ApiModelProperty(value = "角色Id", example = "1")
@NotNull(message = "标签组的Id不能为空")
private Long roleId;
@ApiModelProperty(value = "用户名", required = true)
@NotBlank(message = "用户名不能为空")
private String username;
@ApiModelProperty(value = "密码", required = true)
@NotBlank(message = "密码不能为空")
private String password;
}