好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SpringBoot @NotBlank错误的解决方案

SpringBoot @NotBlank错误

java 验证出现如下错误:

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint

错误原因

Java实体类中属性是Integer类型,用了NotBlank判断不能为空,而这个注解是判断字符串是否为空

解决办法

去掉@NotBlank注解、使用@NotNull

@NotBlank注解地正确使用

@NotNull :不能为null,但可以为empty

@NotEmpty :不能为null,而且长度必须大于0

@NotBlank :只能作用在String上,不能为null,而且调用trim()后,长度必须大于0

案例

?

1

2

3

String name = null ; @NotNull : false @NotEmpty : false @NotBlank : false 2 .String name = "" ; @NotNull : true

@NotEmpty : false @NotBlank : false3.String name = " " ; @NotNull : true

@NotEmpty : true @NotBlank : false4.String name = "Great answer!" ; @NotNull : true @NotEmpty : true @NotBlank : true

注意在使用@NotBlank等注解时,一定要和@valid一起使用,不然@NotBlank不起作用

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/u010096624/article/details/109294547

查看更多关于SpringBoot @NotBlank错误的解决方案的详细内容...

  阅读:23次