• Spring boot 下使用 Swagger


    通过Swagger 可以更好的将后台的RESTfull API文档化,如下图所示:

    1. Swagger 主要依赖以下两个jar包:

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

     2. 建立一个与Spring Boot的启动类同级的类:Swagger .class,在启动项目后,就会自动扫描com.example.demo下的api,到此这就可以了。

    package com.example.demo;
    
    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;
    
    /**
     *  @Description:
     *  @author miaoying
     *  @date 2018/5/23
     */
    @Configuration
    @EnableSwagger2
    public class Swagger {
    
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.example.demo"))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("title test")
                    .description(" description test")
                    .termsOfServiceUrl("url test")
                    .contact("miaoying")
                    .version("1.0.0")
                    .build();
        }
    }

     源码见:https://github.com/mistbor/swagger

  • 相关阅读:
    jetty服务器
    好久不用的正则表达式
    mysql技术调优资料整理
    .net互转java 转行必备
    docker学习资料整理(持续更新中..)
    tcpdump来抓取执行的sql语句
    DDoS deflate
    Linux网络相关查询脚本
    linux下无法删除文件的原因
    linux使用FIO测试磁盘的iops 【转载】
  • 原文地址:https://www.cnblogs.com/miaoying/p/9078609.html
Copyright © 2020-2023  润新知