• SpringBoot整合Swagger2


    Swagger2文档API

    为了减少程序员撰写文档的时间、提高生产里,Swagger2应运而生。

    1. 引入依赖

      <!--Swagger2-->
      <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.4.0</version>
      </dependency>
      
      <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.4.0</version>
      </dependency>
      
      <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.6</version>
      </dependency>
      
    2. config中配置Swagger2.java

    package com.imooc.config;
    
    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.swagger2.annotations.EnableSwagger2;
    
    /**
     * @ClassName Swagger2
     * @Description TODO
     * @Author zhangshao
     * @Date 2020/4/19 12:32
     **/
    @Configuration
    @EnableSwagger2
    public class Swagger2 {
        // http://localhost:8088/swagger-ui.html        原路径
        // http://localhost:8088/doc.html   bootstrap 路径
        //配置Swagger2核心配置 docket
        @Bean
        public Docket createRestApi(){
            return new Docket(DocumentationType.SWAGGER_2)  //指定API类型为swagger2
                    .apiInfo(apiInfo())        //用于定义api文档汇总信息
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.imooc.controller"))  //指定controller包
                    .paths(PathSelectors.any()) //指定所有controller
                    .build();
        }
        private ApiInfo apiInfo(){
            return new ApiInfoBuilder()
                    .title("天天吃货 电商平台接口api")      //文档页标题
                    .contact(new Contact("imooc","https://www.imooc.com","abc@imooc.com")) //联系人信息
                    .description("专为天天吃货提供的api文档")
                    .version("1.0.1")     //文档版本号
                    .termsOfServiceUrl("http://www.imooc.com")     //网站地址
                    .build();
        }
    }
    
    
    1. Controller注解样例

      @Api(value = "商品接口", tags = {"商品信息展示的相关接口"})
      @RestController
      @RequestMapping("/items")
      public class ItemController extends BaseController {
      public IMOOCJSONResult refresh(
                  @ApiParam(name = "itemSpecIds", value = "拼接的规则Ids", required = true,example = "1001,1003,1005")
                  @RequestParam String itemSpecIds){
              if(StringUtils.isBlank(itemSpecIds)){
                  //直接显示购物车数据为空
                  return IMOOCJSONResult.ok();
              }
              List<ShopCartVO> list = itemService.queryItemsBySpecIds(itemSpecIds);
              return IMOOCJSONResult.ok(list);
      }
      
    2. 运行查看效果.

      bootstrap样式的Swgger2文档请求:http://localhost:8088/doc.html

      SiaUFy

  • 相关阅读:
    PyQt4信号与槽
    Amazon Redshift数据库
    NoSQL数据库的认识
    如何划分子网
    VPC见解
    Linux之添加交换分区
    MySQL基础之 标准模式通配符
    MySQL基础之 LIKE操作符
    MySQL基础之 AND和OR运算符
    MySQL基础之 索引
  • 原文地址:https://www.cnblogs.com/shine-rainbow/p/12770883.html
Copyright © 2020-2023  润新知