#Sawgger2--SpringBoot集成Swagger

在SpringBoot框架中集成Swagger更加简单,下面是在SpringBoot中集成Swagger的操作流程:
(1)引入jar包依赖

<!-- 集成swaggerUI -->
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.5.0</version>
</dependency>
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.5.0</version>
</dependency>

(2)Swagger工具类

package com.yzh.maven.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
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;
import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;
/**
 * @Description: Swagger基础配置
 * @since 2018年1月3日
 * @author yucc
 * @version 1.0.1
 */
@Configuration
@EnableSwagger2
@SuppressWarnings({ "deprecation", "unchecked" })
@ComponentScan(basePackages = { "com.yzh" })
public class SwaggerConfig {

   /**
    * @Title: testApi 
    * @Description: 测试
    * @return Docket
    * @author 
    * @date 2018年09月16日
    */
   @Bean
   public Docket testApi() {
      return new Docket(DocumentationType.SWAGGER_2).groupName("test").genericModelSubstitutes(DeferredResult.class)
            .useDefaultResponseMessages(false).forCodeGeneration(true).pathMapping("/")// base,最终调用接口后会和paths拼接在一起
            .select().paths(or(regex("/.*")))// 过滤的接口
            .build().apiInfo(testApiInfo());
   }

   /**
    * @Title: demoApi 
    * @Description: demo
    * @return Docket
    * @author 
    * @date 2018年09月16日
    */
   @Bean
   public Docket demoApi() {
      return new Docket(DocumentationType.SWAGGER_2).groupName("demo").genericModelSubstitutes(DeferredResult.class)
            .useDefaultResponseMessages(false).forCodeGeneration(false).pathMapping("/")
            .select().paths(or(regex("/.*")))// 过滤的接口
            .build().apiInfo(demoApiInfo());
   }

   /**
    * @Title: testApiInfo 
    * @Description: 测试信息
    * @return ApiInfo
    * @author 
    * @date 2018年09月16日
    */
   private ApiInfo testApiInfo() {
      ApiInfo apiInfo = new ApiInfo("Electronic Health Record(EHR) Platform API", // 大标题
            "EHR Platform's REST API, all the applications could access the Object model data via JSON.", // 小标题
            "0.1", // 版本
            "NO terms of service", "1440194053@qq.com", // 作者
            "The Apache License, Version 2.0", // 链接显示文字
            "http://www.apache.org/licenses/LICENSE-2.0.html"// 网站链接
      );
      return apiInfo;
   }

   /**
    * @Title: demoApiInfo 
    * @Description: API信息
    * @return apiInfo
    * @author 
    * @date 2018年09月16日
    */
   private ApiInfo demoApiInfo() {
      ApiInfo apiInfo = new ApiInfo("Electronic Health Record(EHR) Platform API", // 大标题
            "EHR Platform's REST API, for system administrator", // 小标题
            "1.0", // 版本
            "NO terms of service", "1440194053@qq.com", // 作者
            "The Apache License, Version 2.0", // 链接显示文字
            "http://www.apache.org/licenses/LICENSE-2.0.html"// 网站链接
      );
      return apiInfo;
   }

   @Bean
    public Docket customImplementation(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yzh"))
                .build()
                .apiInfo(testApiInfo());
    }
}

(3)运行
启动项目后,在浏览器的地址栏输入下面的url进行访问:
http://localhost:端口/项目名/swagger-ui.html

(4)效果图

这里写图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值