Swagger-ui与springBoot的结合

本文详细介绍如何在SpringBoot项目中集成Swagger2,包括引入依赖、配置启动类及使用注解等步骤,助您轻松搭建API文档。

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

在我们开发的时候,测试接口很多时候都需要写测试类来测试,但是现在有一个非常方便的工具用于我们测试我们开发的接口是否可用,那就是swagger-ui,他的地址是:https://swagger.io/download-swagger-ui/

swagger-ui和springBoot的搭配,特别方便,操作步骤如下。

1:在pom.xml中准备相应的jar包地址:

              <!-- swagger2配置pom开始 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>

<!-- swagger2配置pom结束 -->

2:和springBoot的启动代码同一层添加一个swagger2.java文件用于说明我们启动的内容,代码如下:

package com.zhm.springboot;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zhm.springboot.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("仅用于开发环境")
                .termsOfServiceUrl("http://97kankan.xin/")
                .contact("walle")
                .version("1.0")
                .build();
    }

}

红色字体说明swagger和controller的关联关系,这个地方要注意。很多时候都会忘记这个地址。

3:在controller层中添加标记

常用的swagger注解 
Api 
ApiModel 
ApiModelProperty 
ApiOperation 
ApiParam 
ApiResponse 
ApiResponses 
ResponseHeader 

具体可以参考swagger官方注解文档:http://docs.swagger.io/swagger-core/apidocs/index.html

这样swagger就和springBoot结合在一起了,如果该博文对你有帮助记得收藏。

<think>好的,我现在需要帮用户找到Spring Boot 3.3.4兼容的Swagger版本。首先,我应该回忆一下Spring BootSwagger集成情况。Swagger主要通过两个库来支持:Springfox和SpringDoc。 用户提到的是Spring Boot 3.3.4,而Spring Boot从3.0版本开始迁移到Jakarta EE 9,包路径从javax变更为jakarta。这会影响很多依赖库的兼容性,特别是那些还没有升级到支持Jakarta的库。 Springfox的最后一个版本是3.0.0,但它似乎没有完全适配Spring Boot 3.x,因为Spring Boot 3.x需要Jakarta EE的支持,而Springfox可能还在使用旧的javax包。因此,可能Springfox不兼容,这时候应该考虑SpringDoc。 SpringDoc的版本方面,我记得SpringDoc 2.x版本支持Spring Boot 3.x。具体来说,SpringDoc 2.2.0开始支持Spring Boot 3.0及以上。所以对于Spring Boot 3.3.4,应该使用SpringDoc 2.x的最新版本,比如2.2.0或更高。需要确认官方文档中的兼容性列表,确保2.2.x确实支持Spring Boot 3.3.4。 另外,用户可能想了解如何配置Swagger,是否需要额外的依赖或设置。例如,SpringDoc需要引入springdoc-openapi-starter-webmvc-ui,同时可能需要调整配置。需要给出具体的Maven或Gradle依赖示例,方便用户直接使用。 还需要提醒用户,访问Swagger UI的路径通常是http://localhost:8080/swagger-ui.html,但使用SpringDoc后路径可能有所变化,比如变成http://localhost:8080/swagger-ui/index.html,这点需要确认。 另外,需要检查是否有已知的问题或注意事项,例如Spring Security的集成是否需要额外配置,或者是否需要启用某些注解。例如,可能需要使用@OpenAPIDefinition注解来定义API的基本信息。 总结步骤: 1. 确认Spring Boot 3.x的兼容性需求(Jakarta EE)。 2. 排除Springfox,推荐SpringDoc。 3. 查找SpringDocSpring Boot 3.3.4兼容的版本,例如2.2.0及以上。 4. 提供依赖配置示例。 5. 给出基本的配置步骤和访问路径。 6. 提示可能的常见问题或配置项。 需要确保引用的资料准确,例如参考SpringDoc的官方文档或GitHub仓库的发布说明,确认版本兼容性。同时,用户提供的引用内容中并没有直接提到Swagger,所以可能需要依靠外部知识,但应确保信息的正确性。 最后,生成相关问题,帮助用户深入理解相关主题,比如如何自定义Swagger文档,或如何处理集成中的常见错误。</think>对于Spring Boot 3.3.4,推荐使用 **SpringDoc OpenAPI 2.x** 系列版本(如 2.2.0+),因为Spring Boot 3.x基于Jakarta EE 9+,而SpringDoc从2.0.0版本开始全面支持Spring Boot 3.x的兼容性[^1]。 ### 配置步骤 1. **添加依赖**(Maven示例): ```xml <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>2.2.0</version> </dependency> ``` 2. **启用Swagger UI**: - 访问路径:`http://localhost:8080/swagger-ui.html` - 若需自定义配置,可在`application.properties`中添加: ```properties springdoc.swagger-ui.path=/swagger-ui.html springdoc.api-docs.path=/api-docs ``` ### 注意事项 - **Springfox不兼容**:传统的`springfox-swagger`(如2.9.2)仅支持Spring Boot 2.x及以下,因未适配Jakarta EE 9+[^2]。 - **注解变化**:SpringDoc使用`@Operation`、`@Tag`等新注解替代Springfox的`@Api`系列注解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值