zoukankan      html  css  js  c++  java
  • Java开发文档Swagger的使用详细教程

    springboot中添加swagger

    版本2.7.0

    版本2.8.0

    使用UI界面不同,个人比较喜欢2.7.0

    1.导入swagger依赖

    <!--swagger依赖-->
    <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>

    2.编写SwaggerConfig配置类

    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    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.service.Contact;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger.web.UiConfiguration;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    /**
     *
     */
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
        @Bean
        public Docket buildDocket() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(buildApiInf()) // .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage(""))// 需要生成文档的包的位置
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo buildApiInf() {
            return new ApiInfoBuilder()
                    .title("电影接口详情")
                    .description("Zuul+Swagger2构建RESTful APIs")
                    .version("1.0")
                    .build();
        }
    }

     3.使用注解

    @Api:修饰整个类,描述Controller的作用
    @ApiOperation:描述一个类的一个方法,或者说一个接口
    @ApiParam:单个参数描述
    @ApiModel:用对象来接收参数
    @ApiProperty:用对象接收参数时,描述对象的一个字段
    @ApiResponse:HTTP响应其中1个描述
    @ApiResponses:HTTP响应整体描述
    @ApiIgnore:使用该注解忽略这个API
    @ApiError :发生错误返回的信息
    @ApiImplicitParam:一个请求参数
    @ApiImplicitParams:多个请求参数
  • 相关阅读:
    信号
    13. 罗马数字转整数
    ES6基础-constructor与super
    关于antd-vue动态表单的问题以及解决方案
    (vue)关于在ui框架方法中传入多个参数的解决方案
    ts-4: 类型别名与interface
    ts-3:元组与类型约束
    TS-2:类型注释与类型推断、函数参数的定义与返回类型的定义、对象成员与数组成员的定义方法、类型别名与类别名
    LeetCode 141. 环形链表 | Python
    LeetCode 459. 重复的子字符串 | Python
  • 原文地址:https://www.cnblogs.com/cosmosray/p/13268796.html
Copyright © 2011-2022 走看看