SpringBoot整合swagger2

项目结构

一如我以前风格,首先我们看下项目结构,让我们大致了解下demo里面都有哪些类和配置(部分类和包被我擦掉了,因为和本文无关)

pom配置

这里主要的就是添加了swagger2的jar包,用过maven的都明白,不明白的先补maven相关知识

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <springfox-swagger2.version>2.5.0</springfox-swagger2.version>
    <fastjson.version>1.2.12</fastjson.version>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
</parent>

<dependencies>

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

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

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

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

    <!-- http://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${springfox-swagger2.version}</version>
    </dependency>

    <!-- http://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${springfox-swagger2.version}</version>
    </dependency>

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>${fastjson.version}</version>
    </dependency>

</dependencies>

SwaggerConfig

这个就不说了,看看名字其实就知道是啥意思了吧,但是我还是说一下吧,如果你要使用swagger2,那么你就需要创建这么一个类, 
类的名字无所谓,但是必须用EnableSwagger2这个注解来标注,且类里面有一个@Bean返回的实体类是Docket

@Configuration
@EnableSwagger2
@ComponentScan("com.boyi.example.api.rest")
public class SwaggerConfig {

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

    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfoBuilder().title("Spring Boot Sample REST APIs")
                .description("The APIs here demonstrate creating a service built with Spring Boot")
                .license("MIT")
                .licenseUrl("http://opensource.org/licenses/MIT")
                .contact(new Contact("稻草鸟人", "www.boyi.com", "75999267@qq.com"))
                .version("1.0")
                .build();

        return apiInfo;
    }
}

HomeController

项目中的其他配置

最早贴出来的项目结构中的文件,我们这里没有全部展示出来,因为基本上都是和一般的spring boot是一模一样的,这里就不贴一大推乱乱的代码了

访问swagger ui

根据我们项目的端口,我们在浏览器输入类似如下的地址: http://localhost:8080/swagger-ui.html 
这里我们可以展开,里面详细的记录了我们对外API所需的参数以及返回值,并且可以模拟HTTP请求

参考文档

【1】http://springfox.github.io/springfox/

本文基于署名-非商业性使用-相同方式共享 4.0许可协议发布,欢迎转载、使用、重新发布,但务必保留文章署名稻草鸟人(包含此订阅号二维码)

评论

暂无评论