gpt4 book ai didi

jax-rs - JAX-RS 支持验证组吗?

转载 作者:行者123 更新时间:2023-12-02 09:53:08 52 4
gpt4 key购买 nike

来自 JSR-339:

For simplicity, JAX-RS implementations are NOT REQUIRED to support processing groups other than Default.

这严重限制了 JAX-RS 中验证的有用性,因为例如,对于创建和更新,您通常使用相同的模型对象,但对于创建,不应提供对象的 ID,而对于更新,应提供 ID,可以使用验证组轻松验证。一般来说,在多个流程中使用的所有模型对象都不可能得到验证。

我不理解简单性论点,因为 Bean Validation 已经支持组,因此 JAX-RS 实现只需要将组传递给 Bean Validation 实现(如 Hibernate Validator)。

那么有计划向 JAX-RS 添加验证组吗?

最佳答案

事实证明它确实支持验证组。来自同一个 JSR-339:

The presence of @Valid will trigger validation of all the constraint annotations decorating a Java bean class. This validation will take place in the Default processing group unless the @ConvertGroup annotation is present.

例如,这是如何在我的自定义 CreateUpdate 组而不是 Default 组中验证帐户 bean:

@POST
@Consumes(MediaType.APPLICATION_JSON)
Response createAccount(@Valid @ConvertGroup(from = Default.class, to = Create.class)
Account account)

@POST
@Consumes(MediaType.APPLICATION_JSON)
Response updateAccount(@Valid @ConvertGroup(from = Default.class, to = Update.class)
Account account)

public class Account {

@Null(groups = Create.class)
@NotNull(groups = Update.class)
private String Id;

}

public interface Create {}

public interface Update {}

关于jax-rs - JAX-RS 支持验证组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41924252/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com