JAVA解决接口参数+swagger的参数显示

【关于Swagger的接口入参问题以及返回数据问题】

00后博主,正在自学中,有什么想法大家一起分享
🤳博主主页:Copping0606 😁
博主定期更新🤦‍♂️
谢谢你那么好看还关注我💖
------------------------------------------------------------------------------------分割线-----------------------------------------------------------------------------

相信很多小伙伴在用swagger时,也会出现如下一些问题

1. 参数问题

我们先papa敲了如下代码:

@RestController
@CrossOrigin //解决部分跨域问题
@RequestMapping(value="/student")
@Api(tags = "学生信息接口", value = "学生信息Open api 入口")
pulic class LoginController{
	@Autowired
	private StudentService studentService;
	@RequestMapping(value = "/students")
	@ApiOperation(value = "查看所有样品信息", response = ResultModel.class, httpMethod = "GET")
	public ResultModel findStudents(StudentEntity studentEntity){//userEntity里面包含了分页的属性<page,limit,以及>,这里写法有很多,后面讲
		//......省略一些参数校验
		List<StudentEntity> list=studentService.findStudents(studentEntity);
		return ResultModel.getResultModel(list);//ResultModel为自己定义的返回数据格式
	}
}

这时我们去看swagger的页面http://localhost:(port)/swagger-ui.html#/
(我springboot框架,记得给启动类加 @EnableSwagger2 注解)

我们可以看到,我们查询全部学生前端可能只需要传page,limit两个分页参数
但是他把我们的StudentEntity 的所有参数都需要去填写,
而且我们只需要传两个参数啊,怎么办呢?

这时候我们就可以改一下接口的参数写成

@RestController
@CrossOrigin //解决部分跨域问题
@RequestMapping(value="/student")
@Api(tags = "学生信息接口", value = "学生信息Open api 入口")
pulic class LoginController{
	@Autowired
	private StudentService studentService;
	@RequestMapping(value = "/students")
	@ApiOperation(value = "查看所有样品信息", response = ResultModel.class, httpMethod = "GET")
	public ResultModel findStudents(@ApiParam(value = "分页起始位置", example = "0")
                                   @RequestParam Integer page,
                                   @ApiParam(value = "分页获取展示条数", example = "10")
                                   @RequestParam Integer limit,){//userEntity里面包含了分页的属性<page,limit,以及>,这里写法有很多,后面讲
		//......省略一些参数校验
		List<StudentEntity> list=studentService.findStudents(studentEntity);
		return ResultModel.getResultModel(list);//ResultModel为自己定义的返回数据格式
	}
}

这时再去看就只有这两个参数了,可是问题又来了,我现在前端要传一堆参数过来,当然里面全是实体类StudentEntity的参数,但是Entity中的部分参数,又没有我们怎么办呢?
这时我们就需要加入VO类

 	@RequestMapping(value = "/add")
    @ApiOperation(value = "新增学生信息", response = ResultModel.class, httpMethod = "POST")
    public ResultModel add(AddStudentParamVo addStudentParamVo) {
        StudentEntity studentEntity =new StudentEntity();
        studentEntity .setName(FormatNull.nullToString(addStudentParamVo.getName()));
        studentEntity .setStudentNo(FormatNull.nullToString(addStudentParamVo.getStudentNo()));
        studentEntity .setSex(FormatNull.nullToString(addStudentParamVo.getSex()));
        studentEntity .setBirthday(FormatNull.nullToLong(addStudentParamVo.getBirthday()));
		...........
        int result= sampleService.add(sampleEntity);
        return ResultModel.getResultModel(result);//ResultModel为自己定义的返回数据格式
    }

再去看我们就可以可以看到我们的参数都是想要的参数;
现在入参解决了出参呢?
其实也是通过VO去解决

2.报java.lang.NumberFormatException: For input string: “”…错误,

其实就是依赖的错误,swagger2会依赖了一个叫swagger-models 1.5.20的依赖,它只做了是否为null的判断而没有去判断空串的情况,所以我们只需要移除内置的swagger-models,更换为1.5.21版本的就行了,我的如下:

<!-- Swagger  -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
            <exclusions>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-annotations</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-models</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.21</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.21</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

当然swagger还有些地方我也不会d,嘻嘻

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coping0606

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值