SpringBoot集成Swagger

1、新建一个SpringBoot web项目

2、导入Swagger依赖

ps:刚开始我用的3.0.0版本的springfox-swagger2和springfox-swagger-ui,但输入http://localhost:8080/swagger-ui.html之后swagger页面死活加载不出来,总是出现错误页面。后来换成了2.9.2版本之后,页面就可以加载出来了!

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

3、编写一个Hello工程

  • 新建项目结构如下图所示:

 

  • 新建HelloController.java:

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
}
  • 运行SwaggerDemoApplication.java,浏览器输入localhost:8080/hello,出现以下页面说明程序运行成功!

 

4、配置Swagger

  • 如下图所示,在swagger目录下新建配置目录config,在config下新建SwaggerConfig类,配置Swagger。

 

  • SwaggerConfig类内容如下:

@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
}

5、测试运行

浏览器输入http://localhost:8080/swagger-ui.html,结果如下:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值