SpringBoot整合Swagger2

Swagger2是一个用于生成、描述和可视化RESTfulWeb服务接口的框架。它帮助企业统一接口文档标准,尤其在前后端分离的项目中,提供API文档的重要工具。通过添加依赖、配置类和使用特定注解,开发者可以轻松创建和管理API接口文档。Swagger2还支持在线文档预览和模拟HTTP请求功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.什么是Swagger2

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务的接口文档 . 接口: controller相应的路径方法

2.为什么使用Swagger2

为了解决企业中接口(api)中定义统一标准规范的文档生成工具。很多采用前后端分离的模式,前端只负责调用接口,进行渲染,前端和后端的唯一联系,变成了API接口。因此,API文档变得越来越重要。swagger是一个方便我们更好的编写API文档的框架,而且swagger可以模拟http请求调用。

 

3.如何使用接口文档Swagger2

3.1导入相关依赖

<!--swagger2依赖-->
        <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.9.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.7.8</version>
        </dependency>

3.2创建一个配置类 ------swagger2

@Configuration
@EnableSwagger2 //开启swagger注解驱动
public class SwaggerConfig {
    @Bean //把方法返回的数据对象 交于spring容器管理
    public Docket docket(){
        Docket docket = new Docket(DocumentationType.SWAGGER_2).groupName("QY163")
                .apiInfo(getInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.lzx.controller")) //只为com.ykq.controller包下的类生成接口文档
                .build();
        return docket;
    }

    private ApiInfo getInfo(){
        Contact DEFAULT_CONTACT = new Contact("林深见鹿", "http://www.baidu.com", "120@qq.com");
        ApiInfo apiInfo=new ApiInfo("心里测试系统API", "心里测试系统API", "1.1.0", "http://www.jd.com",
                DEFAULT_CONTACT, "志远科技", "http://www.aaa.com", new ArrayList<VendorExtension>());
        return apiInfo;
    }
}

3.3访问swagger在线文档

Swagger-Bootstrap-UIicon-default.png?t=N2N8http://localhost:8080/doc.html访问路径

 

 

3.4 swagger中常用的注解

使用swagger注解对接口参数加以说明。

@Api(tags="")====使用在controller类上

@ApiOperation(value="")====接口方法上 接口方法加以说明

@ApiParam(value = "",name = "",required = true)

@ApiModel====实体类

@ApiModelProperty===>实体类的属性说明

### SpringBoot 整合 Swagger2 示例教程 在 Spring Boot 项目中整合 Swagger2,可以通过引入相关依赖并进行适当的配置来实现。以下是具体的实现步骤和代码示例: #### 1. 添加依赖 需要在项目的 `pom.xml` 文件中添加以下依赖项: ```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> ``` 这些依赖分别用于生成 Swagger 文档和提供 Swagger UI 的可视化界面[^4]。 #### 2. 创建 Swagger 配置类 创建一个配置类,通过注解声明启动 Swagger 功能,并生成 `Docket` 实例: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot 中使用 Swagger2 API 文档") .description("更多详情请参考官方文档") .version("1.0.0") .build(); } } ``` 上述代码中,`@Configuration` 注解声明该类为配置类,`@EnableSwagger2` 注解启用 Swagger 功能(注意:如果使用的是 Swagger3,则无需此注解)。`Docket` 实例定义了 API 文档的元数据和扫描规则[^2]。 #### 3. 启动项目并访问 Swagger UI 完成以上配置后,启动 Spring Boot 项目。默认情况下,可以通过以下地址访问 Swagger UI 界面: ``` http://localhost:8080/swagger-ui.html ``` 如果使用的是 Swagger3,则访问地址可能为: ``` http://localhost:8080/swagger-ui/ ``` #### 4. 测试接口 在控制器中定义一些测试接口,例如: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class ExampleController { @GetMapping("/hello") public String hello() { return "Hello, Swagger!"; } } ``` 启动项目后,在 Swagger UI 界面中可以查看并测试该接口。 --- ### 注意事项 - 如果项目中启用了 `@EnableWebMvc` 或自定义了 `WebMvcConfigurer`,可能会导致 Swagger 不生效。此时需要手动配置静态资源路径[^3]。 - 确保所有依赖版本一致,避免因版本不兼容导致的问题。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值