在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)效果图