验证不同的帖子请求,一个端点

发布于 2025-01-25 12:10:51 字数 713 浏览 4 评论 0原文

两种类型的POST请求被发送到“/数字”端点:

{
    "type": "A",
    "numberOne": 1
}

如何

{
    "type": "B",
    "numberOne": 1,
    "numberTwo": 2
}

验证A type a numberOne的类型不是null,而对于B型,该数字和numberOne和Numbertwo不是null?我能够找到有关如何使用两个不同的端点和验证组实现这一目标的解释,但不能用一个端点来实现这一目标。

@RestController
public class NumbersController {

    @PostMapping("/numbers")
    public Numbers addNumbers(@Valid @RequestBody Numbers numbers) {
        // Create data. 
        return numbers;
    }
}

@Data
public class Numbers {
    private String type;
    @NotNull
    private Integer numberOne;
    @NotNull
    private Integer numberTwo;
}

Two types of POST requests are sent to the "/numbers" endpoint:

{
    "type": "A",
    "numberOne": 1
}

and

{
    "type": "B",
    "numberOne": 1,
    "numberTwo": 2
}

How to validate for type A that numberOne is not null and for type B that numberOne and numberTwo are not null? I was able to find explanations of how to achieve this with two different endpoints and validation groups, but not how to achieve this with one endpoint.

@RestController
public class NumbersController {

    @PostMapping("/numbers")
    public Numbers addNumbers(@Valid @RequestBody Numbers numbers) {
        // Create data. 
        return numbers;
    }
}

@Data
public class Numbers {
    private String type;
    @NotNull
    private Integer numberOne;
    @NotNull
    private Integer numberTwo;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文