首先需要导入Swagger依赖
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency>
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency>
在使用Swagger的Controller中使用注解
@EnableSwagger2
定义Swagger的Bean
@Bean
public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.elong.Controller")) .paths(PathSelectors.ant("/api/*")) .build().apiInfo(apiInfo()); }
private ApiInfo apiInfo() { ApiInfo apiInfo = new ApiInfo( "Autops REST API", "Autops平台接口", "API TOS", "OPS", "xianghui.niu", "License of API", ""); return apiInfo; }
- 主要的注解使用方法
- @Api : 注解Controller,对应UI中一组API 的命名 description: 描述信息, tag: 标签
- @ApiOperation value: api的功能介绍
- @ApiImplicitParam(name = "id", value = "执行单号", paramType = "query")
- @ApiResponses(value = {@ApiResponse(code = 200, message = "接口调用成功")})