有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Swagger+Spring Boot,如何设置基于Spring Boot验证在API中传递的示例值

在我使用DTO传递信息的所有API调用中,在示例值中,我看到了DTO的所有字段,并在我的Spring Boot应用程序中对DTO应用了验证组,如

    @NotNull(message = "error_card_expiry_month_required", groups = AddUpdateCard.class)
    @JsonProperty("month")
    private Integer Month;

    @NotNull(message = "error_card_expiry_year_required", groups = AddUpdateCard.class)
    @JsonProperty("year")
    private Integer Year;

    @NotBlank(message = "error_card_cvv_required",groups = AddUpdateCard.class)
    @JsonProperty("cvv")
    private String Cvv;

这里我使用了groups = Class.class进行验证

如果可能的话,我如何告诉swagger使用它,以便它不会将整个DTO显示为示例值,而只显示我已验证的值

我只需要该API的已验证的@NotNull注释值

我是否有可能或必须使用任何其他方法来实现这一点


共 (1) 个答案

  1. # 1 楼答案

    尝试将以下代码添加到每个字段

    @ApiModelProperty(name = "The chosen month", position = 0, example = "6")
    @NotNull(message = "error_card_expiry_month_required", groups = AddUpdateCard.class)
    @JsonProperty("month")
    private Integer Month;
    

    swagger使用@ApiModelProperty为给定属性创建DTO(requestbody)

    • 名称:swagger应在模型中显示为信息标记的名称
    • 位置:此属性在requestbody中的顺序
    • 示例:swagger中requestbody字段中的预填充示例

    它可能无法根据bean验证对有效的示例进行排序,但因为您知道自己的约束,所以可以选择您想要的任何示例值