如何将@notnull与Spring Boot一起使用? 我有这种依赖性:

问题描述 投票:0回答:2
拥有它的版本由

管理

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent>
我有这个:

import javax.validation.constraints.NotNull; //other imports ommited @Component @ConfigurationProperties(prefix = "collector") public class CollectorProperties { @NotNull private String urlCDI; //getters and setters }
我的

SpringApplication.run

班有这个pice:
@SpringBootApplication
@EnableConfigurationProperties
@ComponentScan({ "otherPackages", "packageWhereCollectorPropertiesIs" })

当我有这条线时我有我的
application.properties

collector.urlCDI=https://www.cetip.com.br/Home

它可以像其他春豆内的那样工作:

//@Component class variables:
@Autowired
private CollectorProperties props;

   //inside some method
    URL url = new URL(props.getUrlCDI());

但是,当我将其删除或更改属性密钥时,我会得到很多NPE而不是验证错误。我在做什么错? 
hibernate-validator

不包含

javax.validation.constraints.NotNull
接口的实现?

add´@quarted'对您的属性class的注释
    

在控制器中使用Jakarta
validation spring-boot bean-validation
2个回答
8
投票
注释时,我也有类似的问题。 在这种情况下,只有雅加达验证效果。但是,如果您使用

@Validated

注释,那么春季和雅加达注释都将起作用

0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.