zoukankan      html  css  js  c++  java
  • SpringBoot整合Swagger框架

     

    Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。

    总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法、参数和模型紧密集成到服务器端的代码,允许 API 来始终保持同步。Swagger 让部署管理和使用功能强大的 API 从未如此简单。

    引入maven依赖

      <!-- 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>
            <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.10</version>
                <scope>provided</scope>
            </dependency>

    创建配置类

    SwaggerConfig.java
    package com.example.demo.config;
    
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.env.Environment;
    import org.springframework.core.env.Profiles;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Contact;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    import java.util.ArrayList;
    
    /**
     * @author yvioo。
     */
    @Configuration
    @EnableSwagger2  //开启Swagger2
    public class SwaggerConfig {
    
    
        /**
         * 配置Swagger的Docket的bean实例
         * @return
         */
        @Bean
        public Docket docket(Environment environment) {
    
            //设置只在开发中环境中启动swagger
            Profiles profiles=Profiles.of("dev");
    
            //表示如果现在是dev环境,则返回true 开启swagger
            boolean flag=environment.acceptsProfiles(profiles);
    
    
    
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    //是否启动swagger 默认启动
                    .enable(flag)
                    //所在分组
                    .groupName("yvioo")
                    .select()
                    //指定扫描的包路径
                    .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                    //指定扫描的请求,这里表示扫描 /hello/ 的请求
                    //.paths(PathSelectors.ant("/hello/**"))
                    .build();
        }
    
    
        /**
         * 配置ApiInfo信息
         * @return
         */
        private ApiInfo apiInfo() {
    
            //作者信息
            Contact author = new Contact("yvioo", "https://www.cnblogs.com/pxblog/", "111@qq.com");
    
    
            return new ApiInfo(
                    "Swagger测试",
                    "Swagger描述",
                    "1.0",
                    "urn:tos",
                    author,
                    "Apache 2.0",
                    "http://www.apache.org/licenses/LICENSE-2.0",
                    new ArrayList()
            );
    
        }
    }

    测试用户实体类

    User.java

    package com.example.demo.entity;
    
    
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.Data;
    
    @ApiModel("用户实体类 User")  //增加实体类接口注释
    @Data   //使用Lombok插件自动生成get set方法,这样才能在swagger中显示属性值
    public class User {
    
        @ApiModelProperty("用户ID")  //增加字段接口注释
        private Integer id;
    
    
        @ApiModelProperty("用户名")
        private String username;
    
    }

    测试控制器

    SwaggerController.java

    package com.example.demo.controller;
    
    
    import com.example.demo.entity.User;
    import io.swagger.annotations.ApiOperation;
    import io.swagger.annotations.ApiParam;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class SwaggerController {
    
    
        @GetMapping("/hello")
        public String hello(){
            return "hello";
        }
    
    
        /**
         * 接口返回值含有实体类,就会被swagger扫描
         *
         * @return
         */
        @ApiOperation("查询用户方法注释")
        @GetMapping(value = "/get/{id}")
        public User get(@ApiParam("请求参数注释") @PathVariable(value = "id")Integer id){
            return new User();
        }
    }

    使用dev环境 启动项目后  浏览器打开 http://localhost:8081/swagger-ui.html#/   我这里用的端口是8081

    显示效果

  • 相关阅读:
    【笔记】求数据前n个主成分以及对高维数据映射为低维数据
    使用sklearn中的fetch_mldata的错误情况以及可能可行的解决方法
    【笔记】求数据的对应主成分PCA(第一主成分)
    【笔记】主成分分析法PCA的原理及计算
    【笔记】如何确定梯度计算的准确性以及调试梯度下降法
    【笔记】随机梯度下降法
    【笔记】线性回归中的梯度下降法(实现以及向量化并进行数据归一化)
    AttributeError: module 'numpy' has no attribute 'num'
    灵雀云CTO陈恺:从“鸿沟理论”看云原生,哪些技术能够跨越鸿沟?
    容器云在证券行业的探索与实践
  • 原文地址:https://www.cnblogs.com/pxblog/p/12942825.html
Copyright © 2011-2022 走看看