Swagger是一个开源的接口配置文档,通常用于先后端分离,代替后端人员为前端人员书写繁琐的接口文档,使后端人员从繁琐的接口文档中解脱出来。Swagger如何使用呢?首先咱们要在springboot中的pom文件引入依赖包html
<!-- swagger依赖组件 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
其次,添加swagger的配置文件,该配置文件定义了网页访问swagger2的路径以及标题,描述等信息前端
package com.xash.quartzDemo.config; import org.springframework.beans.factory.annotation.Configurable; import org.springframework.context.annotation.Bean; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; 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; @Configurable public class SwaggerConfig{ @Bean public Docket api(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("测试模块") .apiInfo(getApiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.xash.quartzDemo.controller")) .paths(PathSelectors.regex(".*/.*")) .build(); } private ApiInfo getApiInfo(){ return new ApiInfoBuilder() .title("测试模块") .description("小标题") .version("1.0") .build(); } @Bean public Docket api1(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("测试模块1") .apiInfo(getApiInfo1()) .select() .apis(RequestHandlerSelectors.basePackage("com.xash.quartzDemo.controller")) .paths(PathSelectors.regex(".*/.*")) .build(); } @Bean private ApiInfo getApiInfo1(){ return new ApiInfoBuilder() .title("测试模块1") .description("小标题") .version("1.0") .build(); } }
这样,咱们就能够在要添加接口文档的地方利用注解添加相应的接口文档信息了web
例如:spring
@Api(tags="这是个测试Controller")
public class PermissionController {}类上加注解,说明该类具备的功能后端
@ApiOperation(value = "根据用户id查询权限")
public String selectPermissionById(ModelMap map,@ApiParam("id") @RequestParam("id")int id){
System.out.println("开始查询");
map.put("name", "欢饮使用thymeleaf模板引擎");
map.put("sysPermission", permissionService.selectPermissionById(id));
return "index";
}方法上加注解,表示方法的功能,以及可见在形参上加注解,指定形参,对形参进行描述api
访问地址默认为192.168.2.199:8080/项目应用/swagger2-ui.htmlspringboot