You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Spring Boot 3.2中让Swagger显示实体控制器?

解决方案

1. 修正依赖配置

Spring Boot 3.x 版本需要同时引入 springdoc-openapi-starter-webmvc-ui 和 springdoc-openapi-starter-data-rest 依赖——前者负责Swagger UI渲染,后者提供Spring Data REST接口的OpenAPI文档支持。你之前仅引入了webmvc-ui,缺少data-rest的starter,这是核心问题。

替换为以下依赖:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.2.0</version>
</dependency>
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-data-rest</artifactId>
    <version>2.2.0</version>
</dependency>

注意:你之前尝试的springdoc-openapi-ui:1.7.0是适配Spring Boot 2.x的版本,与Spring Boot 3.x不兼容,必须使用starter系列依赖。

2. 启用Spring Data REST的OpenAPI支持

在application.properties或application.yml中添加配置,确保SpringDoc扫描Spring Data REST自动生成的接口:

application.properties

# 启用Spring Data REST的OpenAPI文档生成
springdoc.data-rest.enabled=true
# 指定Spring Data REST的基础路径(默认是/api,可根据你的需求调整)
spring.data.rest.base-path=/api

application.yml

springdoc:
  data-rest:
    enabled: true
spring:
  data:
    rest:
      base-path: /api

3. 验证Repository配置

确保TransactionRepository上的@RepositoryRestResource注解未设置exported=false(默认值为exported=true,未显式配置则无需修改)。若需自定义接口路径,可显式指定:

@RepositoryRestResource(path = "transactions", collectionResourceRel = "transactions")
public interface TransactionRepository extends JpaRepository<Transaction, String> {
    // 你的自定义查询方法...
}

4. 验证结果

启动应用后,访问默认Swagger UI地址http://localhost:8080/swagger-ui.html,即可看到/api/transactions相关的所有接口——包括Spring Data REST自动生成的CRUD接口,以及你自定义的查询方法对应的接口。


内容的提问来源于stack exchange,提问作者Robert

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 01:10:29