Swagger 是一款RESTFUL接口的、基于YAML、JSON语言的文档在线自动生成、代码自动生成的工具。
Swagger介绍
官网介绍:最好的API是使用Swagger工具构建的。
Swagger是一套围绕OpenAPI规范构建的开源工具,可以帮助您设计,构建,记录和使用REST API。
主要的Swagger工具包括:
- Swagger Editor - 基于浏览器的编辑器,在其中编写OpenAPI规范。
- Swagger UI - 将OpenAPI规范呈现为交互式API文档。
- Swagger Codegen - 从OpenAPI规范生成服务端和客户端库。
官网:https://swagger.io/
官方文档:https://swagger.io/docs/specification/about/
预览
配置完成后的效果
配置步骤
添加依赖:pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <dependency> <groupId>com.mangofactory</groupId> <artifactId>swagger-springmvc</artifactId> <version>1.0.2</version> </dependency> <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> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.7.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.7.4</version> </dependency>
|
添加SwaggerConfig.java文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package com.weyoung.framework.config;
@Configuration @EnableWebMvc @EnableSwagger2 @ComponentScan(basePackages = "com.weyoung.platform") public class SwaggerConfig {
@Bean public Docket systemApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.weyoung.platform")) .paths(PathSelectors.any()) .build(); }
private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Swagger配置") .description("SSM-NOTE项目的Swagger文档配置") .termsOfServiceUrl("https://www.wanglixia.top/") .version("v1.0.0") .build();
}
@Bean public RequestMappingInfoHandlerMapping requestMappingInfoHandlerMapping() { return new RequestMappingHandlerMapping(); } }
|
spring-mvc.xml
1 2 3 4 5 6 7
| <bean class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration" />
<bean class="com.weyoung.framework.config.SwaggerConfig" />
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/> <mvc:resources mapping="/webjars" location="classpath:/META-INF/resources/webjars/"/>
|
访问URL:http://localhost:8080/ssm-note/swagger-ui.html
使用
1 2 3 4 5 6 7 8 9 10
| @ApiOperation(value = "获取用户组列表-分页", notes = "获取用户组列表-分页", response = SysGroup.class) @RequestMapping(value = "/queryList", method = RequestMethod.POST) @ResponseBody public PageInfo<SysGroup> querySysGroupList4Paging(HttpServletRequest request, HttpServletResponse response, @RequestBody SysGroup sysGroup) throws Exception { Page<SysGroup> page = new Page<>(sysGroup.getOffset(), sysGroup.getLimit()); PageInfo<SysGroup> groupList = sysGroupService.queryUserList4Paging(page, sysGroup); return groupList; }
|
测试
点击页面出现的一项条目,看到
点击Try It Out!
修改参数后点击Exexute
,在下方查看返回结果。