Bean validation not working if name variable is prefixed with underscore

自闭症网瘾萝莉.ら 提交于 2019-12-01 19:18:12

From the JSR 303 specification:

3.2. Constraint declaration

Constraint declarations are placed on classes or interfaces primarily through annotations. A constraint annotation (see Section 2.1), can be applied to a type, on any of the type's fields or on any of the JavaBeans-compliant properties.

(emphasis mine)

From the JavaBeans specification:

8.3.1 Simple properties

By default, we use design patterns to locate properties by looking for methods of the form:

public <PropertyType> get<PropertyName>();
public void set<PropertyName>(<PropertyType> a);

So, with a property name of _testString, it's looking for a getter/setter called get_testString() and set_testString() which doesn't exist in your case.

Your code guidelines conflicts the JavaBeans specficiation and hence JSR-303 bean validation simply won't work when you put the annotation on a property whose name conflicts the JavaBeans specification. JSR-303 can't find the getter/setter associated with the property name and hence won't be able to perform validation when they are called.

Either fix your code guidelines to comply the standards, or put the annotation on the getter instead and live with it. See further also the Standard Java Code Conventions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!