如何在Spring Boot中使用@NotNull?

拉多

我有这种依赖性:

<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
</dependency>

哪个版本由

<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班上有这样的香料:

@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接口的实现吗?

塞巴斯蒂安·赫尔伯特

在您的媒体资源类中添加“ @Validated”注释

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Spring Boot中使用KeyHolder?

如何在Spring Boot中使用CommonsMultipartResolver

如何在Spring Boot中使用CommandLineRunner?

如何在Spring Boot中使用ORDER BY?

如何在 Pandas 中使用 notnull() 过滤多级列?

如何在Spring Boot + Mybatis中使用连接池

如何在 spring-boot 中使用 ElasticsearchRestClientAutoConfiguration?

如何在Spring Boot中使用EntityManager处理异常

如何在Spring Boot中使用Thymeleaf生成XML?

如何在Spring Boot中使用@ConfigurationProperties模拟类

在Spring Boot中如何在JPA中使用Criteria?

如何在Spring Boot中使用JPA的entityManager.getReference?

如何在Spring Boot中使用推土机?

如何在Spring Boot中使用Thymeleaf保存角色?

如何在Spring Boot Controller中使用Jquery AJAX

如何在Spring Boot中使用阻塞队列?

如何在Spring Boot中使用@Valid验证对象?

如何在 Spring Boot 中使用 ST_CONTAINS?

如何在 jupiter Spring Boot 中使用 any()?

如何在 Spring Boot Rest Controller 中使用多个参数?

如何在Spring Boot中使用websocket并响应js?

如何在 Spring Boot 中使用 WebClient 收集列表

如何通过在Kotlin中使用枚举转换为NotNull

如何在Spring Boot中使用spring-security.xml中的配置?

如何在Spring Boot的每个REST调用中使用spring安全性?

如何在Spring Boot中使用Spring托管的Hibernate拦截器?

如何在Spring Boot中使用Spring Web Services动态WSDL生成?

如何在Spring Boot和Spring WebFlux中使用“功能bean定义Kotlin DSL”?

如何在 Spring Boot 和 Spring Data 中使用两个 Cassandra 数据源?