【问题标题】:Spring Boot - Bean Validation 2.0 Validation of a list of objectsSpring Boot - Bean Validation 2.0 验证对象列表
【发布时间】:2018-09-26 10:34:09
【问题描述】:

我正在使用 springBoot 2,我正在尝试通过以下方式验证列表中的对象:

@RequestMapping(value = "/bets",
    produces = {"application/json"},
    consumes = {"application/json"},
    method = RequestMethod.POST
)
void postBets(@RequestBody List<@Valid Bet> bets);

并且 Bet 类在某些属性上有 @NotNull 注释。

import javax.validation.constraints.NotNull;

public class Bet extends BetMessage {

@NotNull
private String categoryName;

@NotNull
private String marketName = null;

@NotNull
private OffsetDateTime startTime = null;

@NotNull
private String betName = null;

我还在构建文件中添加了 spring-boot-starter-validation 工件,但仍然没有进行验证。

作为一种解决方法,我已经在下面的问题(ValidList 类)中实现了流行的答案,并且验证按预期工作;但是我认为我遗漏了一些明显的东西,该解决方案现在是验证库的一部分。

Validation of a list of objects in Spring

【问题讨论】:

标签: java spring-boot bean-validation


【解决方案1】:

您可能想要编写一个包含您的 Bet 列表的包装器,因为这样您的包装器将符合 JavaBean 规范并且可以应用验证。

在这种情况下,下面的答案可能会有所帮助。

@Valid on list of beans in REST service

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 2020-12-21
    • 1970-01-01
    • 2020-04-27
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多