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

Microprofile OpenAPI Swagger UI:如何隐藏Schema字段多余属性?

问题分析与解决方案

你遇到的情况是因为MicroProfile OpenAPI会自动从JPA注解(如@Id、@Basic、@GeneratedValue)中推断并添加额外的Schema属性,比如:

  • @Basic(optional = false)会被解析为nullable: false
  • @GeneratedValue(strategy = GenerationType.IDENTITY)会被标记为readOnly: true
    这些自动推断的属性会和你手动设置的example一起显示在Swagger UI中。

解决方法

要只保留字段类型和example属性,你可以使用MicroProfile OpenAPI 3.0提供的explicitMode属性,强制Schema只包含你显式指定的配置:

修改实体类字段的@Schema注解:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
@Schema(example = "123", explicitMode = Schema.ExplicitMode.EXPLICIT)
private Integer id;

说明

  • Schema.ExplicitMode.EXPLICIT模式下,OpenAPI生成器只会保留你手动定义的Schema属性(这里是example),以及自动推断的字段类型,其他从JPA注解衍生的属性(如nullable、readOnly)会被忽略。
  • 你的pom中已经配置了microprofile-openapi-api:3.0,完全支持该属性。

其他可选方案

如果不想使用explicitMode,也可以手动将不需要的属性设为默认值,但这种方式需要逐个处理属性,效率较低:

@Schema(example = "123", nullable = true, readOnly = false)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 16:25:21