SpringBoot整合Swagger实现在线接口文档
- 创建一个SpringBoot项目,使用maven构建。
- 在pom文件中添加如下依赖
<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>
- 编写配置类
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.naihe.swagger_test.controller."))
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("swagger集成测试")
.contact(new Contact("naihe", "http://www.itnaihe.club:8081", "ruyuan4j@163.com"))
.description("欢迎访问接口文档,这是描述信息")
.version("1.0").build();
}
}
- Controller中使用
@RestController
@RequestMapping("/details")
@Api(value = "详情相关业务接口", tags = "详情业务相关的controller")
public class DetailsController {
@GetMapping("/getDetails")
@ApiOperation(value = "xxx", name = "xxx")
public String getDetails(){
return "details";
}
}
常用注解及其功能描述