Hibernate validator有下面三種註釋來限制輸入參數必須非空
@NotBlank
Validate that the annotated string is not null or empty. The difference to NotEmpty is that trailing whitespaces are getting ignored.
用於字符串,會忽略字符串尾部的空格
@NotNull
The annotated element must not be null
. Accepts any type.
支持任何類型。
@NotEmptyAsserts that the annotated string, collection, map or array is not
null
or empty.
用於字符串(不會忽略字符串尾部的空格),集合,map和array。
如何使用@NotBlank Long id,將會出現javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.lang.Long.錯誤。