SpringBoot整合Swagger2案例,以及报错:java.lang.NumberFormatException: For input string: ""原因和解决办法
原文链接:https://blog.csdn.net/weixin_43724369/article/details/89341949
SpringBoot整合Swagger2案例
先说SpringBoot如何整合Swagger2,然后再说报错问题。
用IDEA新建SpringBoot项目,只需勾选Web即可。
在项目的pom文件中添加Swagger2相关依赖
<!--引入两个Swagger2相关的依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
待依赖导入完成后,在项目启动类中添加启动Swagger2的注解
添加自定义的Swagger2的配置类Swagger2Config
package com.zzz.swagger2; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket; @Configuration
public class Swagger2Config { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage("com.zzz.swagger2.controller"))//指定方法接口都来自controller这个包
.paths(PathSelectors.any())//选any表示给这个controller包下所有的接口都生成文档
.build().apiInfo(new ApiInfoBuilder()
.title("SpringBoot整合Swagger")//生成的接口文档的标题名称
.description("SpringBoot整合Swagger,详细信息......")//文档摘要
.version("1.0.0")//API版本,可以自定义
//文档制作人、个人主页地址、邮箱
.contact(new Contact("Kyo", "https://blog.csdn.net/weixin_43724369", "aaa@gmail.com"))
.description("Kyo的个人博客")//(可以不配置)
.license("The Apache License")//授权信息(可以不配置)
.licenseUrl("http://www.baidu.com")//授权地址(可以不配置)
.build());
}
}
下图是项目启动后,对应上面的配置信息。不过现在项目还没配置完,先往下面看。
接下来添加对应的实体类对象,和控制层方法,模拟增删改查
①User对象
package com.zzz.swagger2.Bean; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; @ApiModel
public class User { @ApiModelProperty("用户的id")
private Long id;
@ApiModelProperty("用户名")
private String username;
@ApiModelProperty("用户的地址")
private String address; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
}
}
添加完User类后,可以启动项目了。
在浏览器中打开:http://locahost:8080/swagger-ui.html#/ 即可
上面的三行注解:
@ApiModelProperty("用户的id")
@ApiModelProperty("用户名")
@ApiModelProperty("用户的地址")
对应接口文档
②UserController,采用Restful风格,模拟对User信息的增删改查操作。
package com.zzz.swagger2.controller; import com.zzz.swagger2.Bean.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*; @RestController
@Api(tags = "用户管理接口")
public class UserController { @ApiOperation("通过用户id查询一个用户")
@ApiImplicitParam(name = "id",value = "用户id",defaultValue = "33")
@GetMapping("/user")
public User getUserById(Long id){
User user=new User();
user.setId(id);
return user;
} @DeleteMapping("/user/{id}")
@ApiOperation("通过用户id删除一个用户")
@ApiImplicitParam(name = "id",value = "用户id",defaultValue = "99")
public Long deleteUserById(@PathVariable Long id){
return id;
} @PutMapping("/user")
@ApiImplicitParams({@ApiImplicitParam(name = "id",value = "用户id",defaultValue = "80"),
@ApiImplicitParam(name = "username",value = "用户名",defaultValue = "李四")})
@ApiOperation("通过用户id更新用户名")
public Long updateUsernameById(Long id, String username){
return id;
} @PostMapping("/user")
@ApiOperation("添加用户")
public User addUser(User user){
return user;
}
}
③再来一个HelloController,里面只有一个简单的返回字符串的方法
package com.zzz.swagger2.controller; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello swagger!";
}
}
6、这时可以启动项目了,顺便说说controller层里的注解是什么意思
二、报错:java.lang.NumberFormatException: For input string: ""原因和解决办法
项目启动之后,打开http:/localhost:8080/swagger-ui.html#/ 后
你会发现控制台报错:
看意思是数值类型转换异常,原因是输入了一个空字符串 ,而且是把String转换成Long类型数值的过程中发生异常。
随便一分析,就知道我们刚刚定义的User类中,有一个Long类型的属性id
而在UserController中,我们给每个方法的参数id,都设置了默认值,即defaultValue。
却唯独没有给最后一个方法的参数id,设置默认值。罪魁祸首见下图:
而根据报错信息来看,系统会自动把我们输入的String类型的id,转换成Long类型的id,再保存成JSON数据。
这个转换过程调用的就是Long.parseLong(),注意要求的是非空字符串!
由于我们没有给这个addUser()这个方法设置默认值,所以系统已启动,就自动尝试把空字符串转换成Long类型数值,所以就报错了。
所以,解决办法就很简单——给参数id设置任意一个默认值。如下:
@PostMapping("/user")
@ApiImplicitParams({@ApiImplicitParam(name = "id",value = "用户id",defaultValue = "00"),
@ApiImplicitParam(name = "username",value = "用户名",defaultValue = "请输入用户名")})
@ApiOperation("添加用户")
public User addUser(User user){
return user;
}
设置完,再启动项目就不会报错了。
SpringBoot整合Swagger2案例,以及报错:java.lang.NumberFormatException: For input string: ""原因和解决办法的更多相关文章
- hadoop ha环境下的datanode启动报错java.lang.NumberFormatException: For input string: "10m"
hadoop ha环境启动start-dfs.sh的时候datanode启动不了,并且报错. [hadoop@datanode2 ~]$ cat /home/hadoop/hadoop-2.7.3/l ...
- Window启动Zookeeper报错java.lang.NumberFormatException: For input string:
用zkServer start命令报如题的错误,改为直接用zkServer启动则ok 还有在window下,myid文件不能是myid.txt,不能带文件格式 dataDir=D:/zookeeper ...
- java.lang.NumberFormatException: For input string: "F"
在通过myBatis执行sql时,报错: java.lang.NumberFormatException: For input string: "F" xml中sql内容为: &l ...
- 执行Hive时出现org.apache.hadoop.util.RunJar.main(RunJar.java:136) Caused by: java.lang.NumberFormatException: For input string: "1s"错误的解决办法(图文详解)
不多说,直接上干货 问题详情 [kfk@bigdata-pro01 apache-hive--bin]$ bin/hive Logging initialized -bin/conf/hive-log ...
- mybatis 报错:Caused by: java.lang.NumberFormatException: For input string
mybatis的if标签之前总是使用是否为空,今天要用到字符串比较的时候遇到了困难,倒腾半天,才在一个论坛上找到解决方法.笔记一下,如下: 转自:https://code.google.com/p/m ...
- MyBatis报错:Caused by: java.lang.NumberFormatException: For input string: "XX"
<select id="sltTreatment" resultType="com.vitaminmd.sunny.core.bo.Treatment"& ...
- maven项目中使用redis集群报错: java.lang.NumberFormatException: For input string: "7006@17006"
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [redis.client ...
- Swagger2异常 java.lang.NumberFormatException: For input string: ""
问题在访问swagger首页时报错: java.lang.NumberFormatException: For input string: "" at java.lang.Numb ...
- java.lang.NumberFormatException: For input string:"filesId"
做项目时候,页面获取出现了这个问题.找了好久一直以为是我字段或者是数据库字段问题导致引起的. 最后才发现是 struts2中jsp我写错了一个参数,一直导致报错.后来改了就好了. 当大家遇到这个问题的 ...
随机推荐
- WIFI模块AP和STA模式分别是什么意思
无线AP(Access Point):即无线接入点,它用于无线网络的无线交换机,也是无线网络的核心.无线AP是移动计算机用户进入有线网络的接入点,主要用于宽带家庭.大楼内部以及园区内部,可以覆盖几十米 ...
- E. Paint the Tree(树形dp)
题:https://codeforces.com/contest/1241/problem/E 题目大意:给你一棵树,每一个点都可以染k种颜色,你拥有无数种颜色,每一种颜色最多使用2次,如果一条边的两 ...
- 关于VLC无法播放rtsp的问题分析
我之前有一篇博客说,怎么通过vlc查日志,方法不知道是不是特别好,传送门:https://www.cnblogs.com/132818Creator/p/11136714.html 虽然在调试窗口上提 ...
- 量化预测质量之分类报告 sklearn.metrics.classification_report
classification_report的调用为:classification_report(y_true, y_pred, labels=None, target_names=None, samp ...
- 85)PHP,PHP处理mysql的函数种类
首先,就我知道的,一共有三种: 自己用过的是mysql和mysqli,还没用过PDO_mysql 有时,随着我们的各种东西版本的更新,会遇到某一个扩展用不了的情形,所以,就有了编写完成相同功能的使用不 ...
- 学习python-20191208(1)-Python Flask高级编程开发鱼书_第03章_数据与flask路由
视频01: 略...... ———————————————————————————————————————————————————————————— 视频02: 搜索需要外部数据源,也就是需要外部的A ...
- WWT在中国:一个改变了人类探索宇宙方式的少年梦想
想象一下,在宇宙中超光速飞行,访问行星.星云.恒星和小行星将是多么美妙的体验.现在,中国的孩子们已经可以坐在屋子里,仰望穹顶,去探索星球之间无穷的奥秘. 在微软研究院.微软亚洲研究院及中国科学院国家天 ...
- java和javac命令
记录一下,今天无意中用到单独编译和执行某个java类,遇到各种Error: Could not find or load main class等问题,解决方案如下其中2和3选其一试试~ 1.javac ...
- Chrome开发者调试工具
参考资料 Chrome Console不完全指南 Chrome使用技巧 Chrome开发工具详解 结束语 工欲善其事,必先利其器.
- ZOJ-1183-Scheduling Lectures
可以用贪心求最小讲课次数,贪心策略也很好想,就是对于任意主题,能早讲就早讲.这种方案的讲课次数一定是最少的,但是不满意指标不一定是最小,然后再利用动态规划求在最少讲课次数前提下的最小不满意指标. 方法 ...