SpringBoot集成Swagge2

视频地址: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.csdn.net/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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值