视频地址:https://www.bilibili.com/video/BV1PE411i7CV?p=47
简单入门
1、新建一个SpringBoot = Web项目(创建的时候勾选上web)
2、导入相关依赖 3.0.0版本之前的
<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>
3、编写一个Hello工程(可以不用写)
@RestController
public class Hello{
@RequestMapping("/hello")
public String hello(){
return "hello, ";
}
}
4、需要配置SwaggerConfig.java
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
}
5、测试运行 http://localhost:8080/swagger-ui.html
关于Swagger新版本(3.0.0)打不开swagger-ui.html
我当时上面两个依赖导入的是最新的3.0.0版本的,导致访问swagger-ui.html报404错误,后来降了版本就可以访问http://localhost:8080/swagger-ui.html
但我觉得新版本应该也可以访问这个页面的,于是就问了下度娘,借鉴下大佬的文章:
https://blog.youkuaiyun.com/qq_15973399/article/details/107436089
解决方法
删除上面的两个依赖,直接用starter的方式,不需要配置SwaggerConfig.java
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
在启动类上添加注解@EnableOpenApi
@SpringBootApplication
@EnableOpenApi
public class SwaggerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerDemoApplication.class, args);
}
}
测试:访问 http://localhost:8080/swagger-ui/index.html,完成。
@some