我们有任何最佳方法检查或验证Java Spring Boot中的字段值
我有大约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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论