立即注册 登录
即时通讯网 返回首页

NowIsGood.的个人空间 http://www.52im.net/?13124 [收藏] [复制] [RSS]

日志

项目集成Swagger

热度 2已有 1600 次阅读2019-05-07 17:19 |个人分类:项目接口集成API

项目集成Swagger

    选型是Swagger,Swagger是一个简单又强大的能为你的Restful风格的Api生成文档工具。在项目中集成这个工具,根据我们自己的配置信息能够自动为我们生成一个api文档展示页,可以在浏览器中直接访问查看项目中的接口信息,同时也可以测试每个api接口。Swagger生成的api文档是实时更新的,你写的api接口有任何的改动都会在文档中及时的表现出来。下面将详细介绍在项目中如何集成Swagger。

一:首先pom.xml添加依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- swagger2 API接口文档,自动生成 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.8.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.8.0</version>
</dependency>
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>


二:新增一个swagger配置文件类
package com.x52im.rainbowchat.http.util;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author krisLi
 * @version 1.0
 * @since 20190507
 */

@Configuration
@EnableSwagger2
public class SwaggerConfig {

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

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("RainbowChat")
                .description("desc")
                .version("1.0")
                .build();
    }

}

三:项目中配置参数


项目集成Swagger


四:运行系统,浏览器中输入 http://localhost:8080/swagger-ui.html

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

返回顶部