@Configuration public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .pathMapping("/") .select() .apis(RequestHandlerSelectors.basePackage("com.xiaozhumby.controller")) //controller类所在的路径 .paths(PathSelectors.any()) .build().apiInfo(new ApiInfoBuilder() .title("权限管理系统") .description("通用的管理系统项目") .version("9.0") .contact(new Contact("小猪","http://localhost:8080","[email protected]")) .license("hello") .licenseUrl("http://www.baidu.com") .build()); } } @SpringBootApplication @EnableTransactionManagement//启用对@Transient的支持(事务管理) @EnableSwagger2//启用swagger的支持 public class BiyeshejiHouduanApplication { public static void main(String[] args) { SpringApplication.run(BiyeshejiHouduanApplication.class, args); } } @Slf4j @CrossOrigin @RestController @RequestMapping("/hh") @Api(tags = "住户的controller类") public class HouseHoldController { @Autowired private HouseHoldService householdService; @Autowired private RoomService roomService; // 添加依赖注入 @Log @PostMapping("/vip") @ApiOperation(value = "新增住户信息接口") public Result save(@RequestBody HouseHold hh) {}使用了<dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>2.3.0</version> </dependency>后上面的代码该怎么修改才能正常运行。
时间: 2025-07-12 20:34:11 浏览: 2
<think>好的,用户的问题是关于在Spring Boot项目中引入springdoc-openapi-starter-webmvc-ui依赖后,如何调整现有的Swagger配置类和注解,以确保兼容性和正常运行。我需要仔细分析这个问题,并给出正确的步骤。
首先,用户提到之前使用的是Springfox Swagger(@EnableSwagger2),现在迁移到SpringDoc。根据引用[2],SpringDoc专为Spring Boot设计,支持OpenAPI 3,所以需要移除旧的Springfox依赖和注解。因此,第一步应该是删除@EnableSwagger2注解,因为SpringDoc不需要它。
接下来,配置类的调整。原来的SwaggerConfig可能使用了Docket bean,而SpringDoc使用OpenAPI bean。所以需要将配置类中的Docket替换为OpenAPI,并调整相应的配置方法。例如,使用OpenAPI的构造函数,设置info、externalDocs等属性。同时,包路径可能需要更新,比如旧的io.swagger.annotations可能换成io.swagger.v3.oas.annotations。
然后,注解的替换。原来的@Api、@ApiOperation等来自Springfox的注解需要替换为SpringDoc对应的注解,如@Tag、@Operation。参数注解如@ApiParam换成@Parameter,模型注解如@ApiModel换成@Schema。这部分需要注意注解包的更改,确保引入正确的包。
另外,配置文件的调整也很重要。根据引用[1],用户可能已经有了一些SpringDoc的配置,比如路径设置。需要检查application.properties或.yml中的相关配置,确保与SpringDoc兼容,比如springdoc.swagger-ui.path是否正确。
最后,依赖管理方面,需要确认pom.xml或build.gradle中移除了Springfox的相关依赖,并正确引入了springdoc-openapi-starter-webmvc-ui的依赖,版本为2.3.0。同时,注意依赖冲突的可能性,确保没有旧版本的Swagger库残留。
可能还需要考虑是否启用了Spring MVC,因为SpringDoc需要webmvc支持。启动类不需要特别修改,但需要确保应用正常启动,并能访问/swagger-ui.html等路径。如果遇到问题,可能需要检查日志,查看是否有配置错误或缺失的注解。
总结步骤:移除旧注解和依赖,更新配置类和注解,调整配置文件,处理依赖关系,最后测试验证。需要确保每一步都正确执行,并参考官方文档进行验证,避免遗漏细节。</think>以下是如何调整现有Swagger配置以适配springdoc-openapi-starter-webmvc-ui的逐步指南:
### 一、配置类改造步骤
1. **删除旧注解**
- 移除`@EnableSwagger2`(SpringDoc无需此注解)
- 移除`springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc`
2. **新建OpenAPI配置类**
```java
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.Contact;
@Configuration
public class OpenApiConfig {
@Bean
public OpenAPI springShopOpenAPI() {
return new OpenAPI()
.info(new Info()
.title("API文档")
.description("SpringDoc集成示例")
.version("v1.0")
.contact(new Contact().name("开发者").email("[email protected]")));
}
}
```
### 二、注解替换对照表
| Springfox (旧) | SpringDoc (新) | 作用域 |
|-------------------------|-----------------------------|---------------|
| `@Api` | `@Tag(name="模块名")` | Controller类 |
| `@ApiOperation` | `@Operation(summary="描述")`| 方法级别 |
| `@ApiParam` | `@Parameter(description="")`| 参数级别 |
| `@ApiModel` | `@Schema` | DTO类 |
| `@ApiModelProperty` | `@Schema(description="")` | DTO字段 |
### 三、关键配置属性示例(application.properties)
```properties
# 基础配置
springdoc.api-docs.enabled=true
springdoc.api-docs.path=/v3/api-docs
springdoc.swagger-ui.path=/swagger-ui.html
springdoc.swagger-ui.enabled=true
# 高级配置
springdoc.swagger-ui.tagsSorter=alpha
springdoc.swagger-ui.operationsSorter=alpha
springdoc.cache.disabled=true
```
### 四、依赖管理要点
1. **必须移除的依赖**
```xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
```
2. **新增依赖(Gradle示例)**
```groovy
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'
```
### 五、验证步骤
1. 访问`http://localhost:8080/v3/api-docs`验证JSON格式
2. 访问`http://localhost:8080/swagger-ui.html`检查UI
3. 检查日志是否出现`Mapped "{[/v3/api-docs],methods=[GET]}"`[^2]
### 六、常见问题处理
- **404错误**:检查`springdoc.swagger-ui.path`是否被拦截
- **注解失效**:确认导入的是`io.swagger.v3.oas.annotations`包
- **版本冲突**:使用`mvn dependency:tree`排查旧版Swagger依赖
阅读全文
相关推荐














