引入依赖项
<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>
新建一个配置类
@Configuration //告诉Spring容器,这个类是一个配置类
@EnableSwagger2 //启动Swagger2功能
public class SwaggerConfig {
//配置Swagger2相关的bean
@Bean
public Docket creatRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com"))
//com包下的所有API都交给Swagger2管理
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("演示项目API")
.description("演示项目")
.version("1.0")
.build();
}
}
运行项目,在浏览器输入127.0.0.1/swagger-ui.html即可访问到
可以点击Try it out调试POST请求