用swagger测试springboot项目中的controller接口
1.swagger环境配置:
在需要使用swagger的模块pom下添加以下配置:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>RELEASE</version> </dependency>
2.新建config目录,新建swaggerconfig类:
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("*****"))//这里配置需要扫描的代码路径 .paths(PathSelectors.any()) .build(); } }
3.在controller类名添加
@Api(tags = "注释***")
具体的方法上添加
@ApiOperation(value = "对方法的描述")
在参数上添加
@ApiParam(value = "对参数的描述")
4.访问swagger界面
http://localhost:8080/swagger-ui.html