一、添加maven依赖
<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配置类
package com.system; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.context.request.async.DeferredResult; 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; @Configuration public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) // .genericModelSubstitutes(DeferredResult.class) // .useDefaultResponseMessages(false) // .forCodeGeneration(true) // .apiInfo(apiInfo()) // .pathMapping("/")// .select() // .apis(RequestHandlerSelectors.basePackage("com.controller")) //这边放你们的controller的包的位置 .paths(PathSelectors.any()) // .build(); // } private ApiInfo apiInfo() { return new ApiInfoBuilder() // .title("swagger构建api文档") // .description("restfun风格") // .termsOfServiceUrl("https://github.com/springfox/springfox-demos") // .version("1.0") // .build(); } }
三、springboot添加注解
四、一个测试用例
@ApiOperation(value = "用户注册", notes = "用户注册", httpMethod = "POST") @ApiImplicitParams({ @ApiImplicitParam(name = "name", value = "用户昵称", required = true, dataType = "string",paramType = "query"), @ApiImplicitParam(name = "account", value = "账号", required = true, dataType = "string",paramType = "query"), @ApiImplicitParam(name = "pwd", value = "密码", required = true, dataType = "string",paramType = "query"), @ApiImplicitParam(name = "rePwd", value = "确认密码", required = true, dataType = "string",paramType = "query") })
一个注册的controller上面添加以上配置就可以了
swagger页面的访问路径 ip:port/swagger-ui.html
里面的要填的参数,上面配置的name会自动映射到controller里面名称相同的参数。