为什么NotNull注释不起作用Spring Boot

nikito55:

我有几个有效的注释,只有@NotNull无效。当我尝试使用空名称保存联系人时,它会保存。如果需要,我可以上传视图文件。
这是我的实体类(仅带有@NotNull批注的部分)。

@Entity

公开课联系方式{

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;

@NotNull
private String name;

}

这是控制器类:

@Controller

公共类ContactController {

@Autowired
private ContactRepository contactRepository;

@GetMapping("/addContact")
public String addContact(Model model) {
    model.addAttribute("contact", new Contact());
    return "addContact";
}

@PostMapping("/add")
public String add(@ModelAttribute @Valid Contact contact, BindingResult bindingResult) {

    if(bindingResult.hasErrors()) {
        return "addContact";
    }
    contactRepository.save(contact);
    return "addContact";
}

}

thomas77:

如注释中所述,请改用注释@NotBlank空字符串不为null-例如String s = ""@NotBlank将要求至少一个非空白字符。

这是javadoc

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring Boot @ApiController 注释不起作用

为什么RestController在Spring Boot中不起作用?

Spring Boot Starter Data Rest,@Notnull约束不起作用

注释CrossOrigin在Spring Boot中不起作用

事务注释在Spring Boot中不起作用

Spring Boot验证注释@Valid和@NotBlank不起作用

Spring Boot-@Value注释不起作用

Spring Boot EnableCaching和可缓存注释不起作用

事务注释在使用JPA的Spring Boot中不起作用

@Transactional注释rollbackFor值在Spring Boot中不起作用

为什么SpEL在Spring Boot和Spring Cloud Stream @SendTo中不起作用

为什么我的Spring Boot错误控制器不起作用?

Spring Boot-为什么@valid在类型控制器上不起作用?

为什么组件扫描对Spring Boot单元测试不起作用?

为什么这个Spring Boot / Thymeleaf按钮隐藏我的数据或根本不起作用?

为什么带有Spring Boot的Keycloak教程不起作用?

为什么AOP在我的Spring Boot应用程序中不起作用?

为什么@PostConstruct 在 Spring Boot 应用程序中不起作用?

为什么在 Spring Boot 项目中使用 flyway 时撤消迁移不起作用?

@NotNull @NotBlank @Valid在Spring Boot Rest API验证中不起作用

Spring ComponentScan excludeFilters注释在Spring Boot Test上下文中不起作用

使用nativeQuery的Spring Boot Query注释在Postgresql中不起作用

Jax-rs 注释路径在 java spring boot 中不起作用?

Spring Boot-表单上的自定义验证注释不起作用

Spring Boot视图不起作用

使用Spring Boot的@AuthenticationPrincipal不起作用

Spring Boot AuthenticationManagerBean不起作用

Spring Boot setContentType不起作用

@RefreshScope不起作用-Spring Boot