spring集成Swagger
1.本例环境
1>.本例demo1源码下载地址: https://github.com/zhangbeizhen/spring-swagger
本例demo2源码下载地址: https://github.com/zhangbeizhen/spring-swagger2
2>. Swagger + spring + mybatis + eclipse Mars + 基于maven的web项目
2.1>. spring集成mybatis的源码下载地址: https://github.com/zhangbeizhen/spring-mybatis
2.2>. spring集成mybatis参考博客地址: https://blog.****.net/zhangbeizhen18/article/details/88575287
2.本例说明
本例是在spring集成mybatis的基础上进行集成Swagger,实现增删改查.
本文只介绍集成Swagger,项目是在可运行的spring基础上集成
3.集成步骤
3.1>.在pom.xml文件引入依赖
<!-- swagger2核心依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<!-- swagger-ui为项目提供api展示及测试的界面 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
3.2>.在spring-mvc.xml配置添加信息
<!-- swagger静态资源访问配置2019-3-26 -->
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
3.3>.SwaggerConfig.java配置类
@EnableWebMvc
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.build().apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("swagger接口文档")
.description("基于restful的接口描述文档")
.version("1.0.0") // 版本
.build();
}
}
3.4>.在Controller类的方法上使用Swagger注解
@GetMapping("/query/{id}")
@ApiOperation(value = "查询员工信息", notes = "查询员工信息")
public Employee getEmployee(@PathVariable("id") Integer id) {
Employee employee = employeeService.getEmployee(id);
return employee;
}
4.使用Swagger
使用url访问: http://127.0.0.1:8080/spring-swagger/swagger-ui.html
截图
5.测试查询
5.1>.点击选择某个方法
5.2>.输入参数,点击Try it out!
5.3>.结果展现
以上,TKS.