springboot引入swagger2
时间: 2023-04-24 15:05:59 浏览: 283
Spring Boot引入Swagger2可以通过以下步骤实现:
1. 在pom.xml文件中添加Swagger2的依赖:
```
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
```
2. 在Spring Boot的启动类上添加@EnableSwagger2注解开启Swagger2:
```
@SpringBootApplication
@EnableSwagger2
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
3. 创建一个Swagger2配置类,配置Swagger2的一些基本信息:
```
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("更多Spring Boot相关文章请关注:http://www.xxx.com/")
.termsOfServiceUrl("http://www.xxx.com/")
.contact("xxx")
.version("1.0")
.build();
}
}
```
4. 启动Spring Boot应用,访问http://localhost:8080/swagger-ui.html即可看到Swagger2的UI界面,可以在这里测试API接口。
阅读全文
相关推荐


















