springBoot集成swagger2

swagger是什么?

1、是一款让你更好的书写API文档的规范且完整框架。

2、提供描述、生产、消费和可视化RESTful Web Service。

3、是由庞大工具集合支撑的形式化规范。这个集合涵盖了从终端用户接口、底层代码库到商业API管理的方方面面。

swagger在springboot中的使用

1.创建一个springboot项目,并添加依赖。在pom.xml添加以下依赖代码。

 <!-- 用于JSON API文档的生成-->
        <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>

如果项目启动后退出,需添加web页面依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2。配置swagger类

@Configuration
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.sw.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot利用swagger构建api文档")
                .description("springboot集成swagger2,https://mp.csdn.net/mdeditor/101295984")
                .termsOfServiceUrl("https://mp.csdn.net/mdeditor/101295984")
                .version("1.0")
                .build();
    }
}

3.在启动类添加依赖注入,启动swagger2。也可以在配置类添加的。

@SpringBootApplication
@EnableSwagger2
public class SwApplication {

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

}

到这里就完成了。接下来是测试。
4.添加测试接口

@RestController
public class TestController {

    @ApiOperation(value="测试接口", notes="测试接口")
    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    public String  jsonTest() {
        return " hi you!";
    }
}

5.启动项目。浏览器浏览http://localhost:8080/swagger-ui.html。这是我运行后的截图。
在这里插入图片描述
6. 我把项目代码上传到码云上了,这是项目地址https://git.oschina.net/yjlcy/springboot-swagger.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值