我们有任何最佳方法检查或验证Java Spring Boot中的字段值

发布于 2025-02-07 13:58:26 字数 562 浏览 1 评论 0原文

我有大约500个字段,我需要检查春季休息端点的JSON字段的值的值,有效值

Class Customer {

    long id;

    String gender; // acceptable values: MALE || FEMALE 

    int validated; // acceptable values: 0 || 1

    String plan;// acceptable values: PLAN1 || PLAN2 || PLAN3 || PLAN4

}

编写自定义方法以检查每个字段,是否有任何通用方法,我们不需要任何通用方法为每个字段或类编写方法验证。

我已经尝试了此 Java字符串使用枚举值和注释值 但是,当值不在可接受的值中时,这不会丢弃错误。基本上,如果值不可接受的值,则应该引发异常

I have around 500 fields where I need to check values coming for the fields from json in spring rest endpoint are having valid values

ex:

Class Customer {

    long id;

    String gender; // acceptable values: MALE || FEMALE 

    int validated; // acceptable values: 0 || 1

    String plan;// acceptable values: PLAN1 || PLAN2 || PLAN3 || PLAN4

}

it will be harder to write a custom method to check each field, is there any generic approach where we dont need to write method validations for each field or class.

I have tried this Java String validation using enum values and annotation
but this is not throwing error when the value is not in the acceptable values. Basically it should be throwing exception if value is other than acceptable value

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

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

发布评论

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