Swagger + SwaggerUI

本文介绍了在SpringBoot2应用中使用Swagger2.9.2时遇到的问题,涉及版本冲突导致的错误,以及如何通过配置类和注解解决并创建API文档。
摘要由CSDN通过智能技术生成

        用的是SpringBoot2、jdk11、<spring-boot.version>2.3.3.RELEASE</spring-boot.version>

        (单纯的swagger还是不如knife界面好用好看)

1.导入依赖

<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.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
</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>

  • swagger 2.9.2 有点问题,每次打开swagger都会报下面这个错
  • java.lang.NumberFormatException: For input string: ""  
  • 看到一个大佬的写的原因如下:

    查阅大量资料发现是swagger版本的问题,使用的是io.springfox:springfox-swagger2:2.9.2的版本,而该版本依赖了swagger-models的1.5.20版本(会导致报此错),深挖原因是1.5.20版本中的example只判断了是否为null,没有判断是否为空串;1.5.21版本对两者都进行了判断。
                            
    原文链接:https://blog.csdn.net/Vicky_2020/article/details/120283394

2.添加配置类

import org.springframework.context.annotation.Configuration;
import springfox.documentation.annotations.ApiIgnore;
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.service.Tag;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author : luobei
 * @date : 2024/4/15 8:56
 * swagger配置文件
 * 访问地址:http://localhost:端口/swagger-ui.html
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    private static final String VERSION="1.0.1";
    private static final String AUTHOR="luobei";
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.luobei.as_api.controller"))
                .paths(PathSelectors.any())
                .build()
                .ignoredParameterTypes(ApiIgnore.class)
                .enableUrlTemplating(false)
                .tags(new Tag("Account", "账号模块"));
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("Api文档")
                .description("Api文档")
                .version(VERSION)
                .contact(new Contact(AUTHOR,"",""))
                .build();
    }
}

3.启动类开启swagger

@SpringBootApplication
@MapperScan("com.luobei.as_api.mapper")
@EnableSwagger2
public class AsApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(AsApiApplication.class, args);
    }

}

4.controller添加注解

@RestController
@RequestMapping("/xxx")
@Api(tags = {"Account"})
public class xxxController {

    @PostMapping("/xxx")
    @ApiOperation("xxx")
    public Object xxx(Xxx xxx){
        return xxx;
    }
}

        实体类的参数上添加注解

@ApiModelProperty(value = "名字") 

        然后就可以访问了

http://localhost:5088/swagger-ui.htmlicon-default.png?t=N7T8http://localhost:5088/swagger-ui.html

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值