本文介绍了在spring boot中使用openapi使用@Schema(allowableValues=)作为枚举参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 API 在请求正文中有一个 enum 字段.当我生成 swagger UI 时,它显示的枚举具有允许的值作为枚举名称.我需要将其设置为枚举值,而不是枚举名称.为此,我使用了 @Schema(allowableValues=).这会在 swagger UI 中生成一个包含值和名称的列表.我可以仅在 swagger 中将其设置为值吗?

My API has a enum field in request body. When I generate swagger UI, its showing enum with allowed value as enum names. Instead of enum names, I need to set it to enum values. For that I used @Schema(allowableValues=). This lead to a list with both values and names in swagger UI. Can I set this to values only in swagger?

我的枚举示例:

 public enum Days{
  MON("Monday"),
  SUN("Sunday")
}

当前 swagger 字段看起来像:

Current swagger field looks like :

日期字符串枚举:[ MON, SUN ]

我想要它:

日期字符串枚举:[星期一,星期日]

当我将 @Schema(allowableValues={"Monday", "Sunday") 添加到枚举字段时,Swagger 变为:

When I add @Schema(allowableValues={"Monday", "Sunday") to the enum field, Swagger becomes:

日期字符串枚举:[MON, SUN, Monday, Sunday]

感谢任何解决方案.

推荐答案

使用

@Schema(type = "string", allowableValues = { "Monday", "Sunday" })

这篇关于在spring boot中使用openapi使用@Schema(allowableValues=)作为枚举参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 13:00