首页 > 其他分享 >【Spring】Bean Validation

【Spring】Bean Validation

时间:2023-08-09 22:58:11浏览次数:44  
标签:Validation annotated Spring validates value Bean hibernate including property

 

参考:

https://www.baeldung.com/java-validation

https://www.baeldung.com/java-bean-validation-not-null-empty-blank

https://www.baeldung.com/spring-mvc-custom-validator  定制自己的校验器


 

maven依赖

In the latest version of spring-boot-starter-validation, besides other dependencies, transitive dependency of hibernate-validator will be available.

If you want to add just the dependency for validation you can simply add the hibernate-validator in you pom.xml.

A quick note: hibernate-validator is entirely separate from the persistence aspects of Hibernate. So, by adding it as a dependency, we're not adding these persistence aspects into the project.

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.10.Final</version>
</dependency>

 

基本注解:

  • @NotNull validates that the annotated property value is not null.
  • @AssertTrue validates that the annotated property value is true.
  • @Size validates that the annotated property value has a size between the attributes min and max; can be applied to StringCollectionMap, and array properties.
  • @Min validates that the annotated property has a value no smaller than the value attribute.
  • @Max validates that the annotated property has a value no larger than the value attribute.
  • @Email validates that the annotated property is a valid email address.

Some annotations accept additional attributes, but the message attribute is common to all of them. This is the message that will usually be rendered when the value of the respective property fails validation.

And some additional annotations that can be found in the JSR:

  • @NotEmpty validates that the property is not null or empty; can be applied to StringCollectionMap or Array values.
  • @NotBlank can be applied only to text values and validates that the property is not null or whitespace.
  • @Positive and @PositiveOrZero apply to numeric values and validate that they are strictly positive, or positive including 0.
  • @Negative and @NegativeOrZero apply to numeric values and validate that they are strictly negative, or negative including 0.
  • @Past and @PastOrPresent validate that a date value is in the past or the past including the present; can be applied to date types including those added in Java 8.
  • @Future and @FutureOrPresent validate that a date value is in the future, or in the future including the present.

 

标签:Validation,annotated,Spring,validates,value,Bean,hibernate,including,property
From: https://www.cnblogs.com/clarino/p/17618215.html

相关文章

  • 解密SpringBoot3.0:构建易维护的JavaWeb应用
    SpringBoot3.0最新深入浅出从入门到项目实战,突出Web应用痛点解决方案SpringBoot已经成为Java开发中最流行的框架之一,它提供了一种快速构建、易于扩展的方式,使开发人员能够更加专注于业务逻辑而不是繁琐的配置。而最新的SpringBoot3.0版本将进一步改善开发体验,并提供更多的解决方......
  • Spring 简介
    Spring是用于企业Java应用程序开发的最流行的应用程序开发框架。全球数百万开发人员使用SpringFramework创建高性能、易于测试和可重用的代码。SpringFramework是一个开源的Java平台。它最初由RodJohnson编写,并于2003年6月在Apache2.0许可下首次发布。Spring在大小和透明度方......
  • Spring Boot 启动流程追踪(第一篇)
    1、初始化SpringApplication publicSpringApplication(ResourceLoaderresourceLoader,Class<?>...primarySources){ this.resourceLoader=resourceLoader; Assert.notNull(primarySources,"PrimarySourcesmustnotbenull"); this.primarySourc......
  • 遥遥领先 spring,中国人的 solon 来啦!10% 的体积,10倍的速度
    Solon是什么?Java生态型应用开发框架。它从零开始构建,有自己的标准规范与开放生态(历时五年,已有全球第二级别的生态规模)。与其他框架相比,它解决了两个重要的痛点:启动慢,费内存。2023年6月,Maven单月下载量突破200万。解决痛点?由于Solon Bean容器的独特设计,不会因为扩展依赖变多......
  • Spring 简介
    Spring是用于企业Java应用程序开发的最流行的应用程序开发框架。全球数百万开发人员使用SpringFramework创建高性能、易于测试和可重用的代码。SpringFramework是一个开源的Java平台。它最初由RodJohnson编写,并于2003年6月在Apache2.0许可下首次发布。Spring在大小和透明度......
  • 扩展SpringMVC框架的消息转化器
    1、消息转化器请求和响应都有对应的body,而这个body就是需要关注的主要数据。请求体与请求的查询参数或者表单参数是不同的,请求体的表述一般就是一段字符串,而查询参数可以看作url的一部分,这两个是位于请求报文的不同地方。表单参数可以按照一定格式放在请求体中,也可以放在url上......
  • Spring Boot 链路追踪 SkyWalking 入门
    1.添加SkyWalking依赖:打开您的SpringBoot项目的pom.xml文件,并在<dependencies>标签中添加以下依赖:xml<dependency><groupId>org.apache.skywalking</groupId><artifactId>apm-toolkit-trace</artifactId><version>8.0.0</ver......
  • SpringMVC支持跨域访问详解
    跨站HTTP请求(Cross-siteHTTPrequest)是指发起请求的资源所在域不同于该请求所指向资源所在的域的HTTP请求。这里有域名的不同,端口号的不同。很多浏览器在发起跨域访问时是会询问用户是否需要发送该请求,或者干脆不发送跨域访问请求。(最好的办法是不使用ajax之类的,不要在前端......
  • SpringBoot启动项目失败但不报错
    新建的SpringBoot项目,点击启动,项目没有启动成功,但是不报错。如下:._________/\\/___'_____(_)______\\\\(()\___|'_|'_||'_\/_`|\\\\\\/___)||_)|||||||(_||))))'|____......
  • 【Spring | 事件监听详解】
    上篇Spring事件监听概述对Spring事件监听的机制有了个基本的了解。本篇来详细的解读下Spring的事件监听机制。(事件监听详解)ApplicationEvent  ApplicationEvent最重要的子类是ApplicationContextEvent抽象类,ApplicationContextEvent是spring容器Context生命周期......