springboot集成swagger的方法是什么
Spring Boot集成Swagger的方法有以下几个步骤:
引入Swagger相关依赖:在pom.xml
文件中添加以下依赖:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
创建Swagger配置类:创建一个Swagger配置类,用于配置Swagger的相关信息和规则。示例代码如下:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build();
}
}
在上述代码中,basePackage
方法指定了需要生成API文档的包路径,可以根据实际情况进行修改。
启用Swagger:在Spring Boot应用主类上使用@EnableSwagger2
注解启用Swagger。示例代码如下:
@SpringBootApplication
@EnableSwagger2
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
访问Swagger UI:启动Spring Boot应用后,可以通过访问http://localhost:8080/swagger-ui.html
来查看生成的API文档。
注意:上述示例中使用的是Swagger 2.x版本,如果使用的是Swagger 3.x版本,需要相应地调整依赖和配置类。
阅读剩余
THE END