用的是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.htmlhttp://localhost:5088/swagger-ui.html