springboot集成Swagger2,解决NumberFormatException异常

一、pom文件添加引入jar包

<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>

二、application.yml文件开启配置
swagger:
open: true
三、swagger配置启动类
package com.fosung.ywpt.paas.config;

import com.google.common.base.Predicates;
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.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
* @author Sun En Ming
* @version V1.0
* @description: Swagger3.0接入配置类
* @date 2022/2/14 10:25
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {

//访问地址 http://localhost:6602/swagger-ui.html

@Bean
public Docket adminApiConfig(){

return new Docket(DocumentationType.SWAGGER_2)
.groupName("adminApi")
.apiInfo(adminApiInfo())
.select()
//只显示admin路径下的页面
.apis(RequestHandlerSelectors.basePackage("com.fosung.ywpt.paas.controller"))
.paths(PathSelectors.any())
.build();

}
private ApiInfo adminApiInfo(){

return new ApiInfoBuilder()
.title("运维平台-后台管理系统-API文档")
.description("本文档描述了后台管理系统服务接口定义")
.version("1.0.0")
.contact(new Contact("fosung", "http://fosung.cn", "xxxx@fosung.com"))
.build();
}

}
四、controller类及接口增加相应swagger注解即可
@RestController
@RequestMapping("api")
@Api("swaggerDemoController相关的api")
public class SwaggerDemoController {}
@ApiOperation(value = "获取摄像头的监控视频流地址")
public ResultDTO getVideoPreviewUrl(){}
可能会遇到的坑

当我们访问Swagger文档时,又发现了一个问题,会报NumberFormatException异常;

  • 原因是当我们使用@ApiModelProperty注解时,作为Long数据类型,如果你不添加example属性,默认值是空字符串,空字符串转型自然就会报NumberFormatException异常;

不过使用新版本的swagger-annotationsswagger-models依赖包就可以解决了,于是我们的Swagger依赖变成了下面这样的;    

<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <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.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
    </dependency>
    <!--解决Swagger 2.9.2版本NumberFormatException-->
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-models</artifactId>
        <version>1.6.0</version>
    </dependency>
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>1.6.0</version>
    </dependency>
</dependencies>



posted @   孟丽  阅读(257)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示