步骤一
pom.xml 依赖配置
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
步骤二
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build().apiInfo(new ApiInfoBuilder()
.title("xx公司API文档")
.description("xx公司API文档")
.version("1.0")
.contact(new Contact("", "", ""))
.license("The Apache License")
.licenseUrl("http://www.baidu.com")
.build());
}
}
步骤三
在 Controller 的类和方法上打注解
类上:
@Api(tags = "接口描述")
方法上:
@ApiOperation("方法名")
@ApiImplicitParams({
@ApiImplicitParam(name = "参数名", value = "描述")
@ApiImplicitParam(name = "name", value = "姓名")
})
本文详细介绍如何在SpringBoot项目中集成Swagger2,包括pom.xml依赖配置、Swagger配置类编写及Controller注解使用,帮助快速生成清晰的API文档。
2482

被折叠的 条评论
为什么被折叠?



