SpringBoot如何为controller参数添加校验呢?
下文笔者讲述SpringBoot为Controller的参数设置验证的方法分享
学习完本篇你将掌握SpringBoot中为Controller参数设置校验的方法分享,如下所示
学习完本篇你将掌握SpringBoot中为Controller参数设置校验的方法分享,如下所示
参数校验的实现思路
参数校验 主要使用两个注解@Validated和@Valid: @Valid是Hibernate的注解校验 @Validated属于spring是@Valid的增强 @Validated和@Valid注解区别: @Valid可标注在成员属性上也可以嵌套校验 @Validated可使用分组校验 参数校验的使用步骤: 1.引入依赖spring-boot-starter-validation 2.添加相应注解例:Controller上加入注解的示例
maven导入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> <version>2.7.5</version> </dependency>
@AssertFalse | 必须为false,支持boolean和Boolean,null是有效的 |
@AssertTrue | 必须为true,支持boolean和Boolean,null是有效的 |
@DecimalMax | 必须是一个小于或者小于等于设定值的数字,可以用inclusive指定是否包含数字(默认true),并且value是String类型的,支持BigDecimal、BigInteger、CharSequence、byte / short / int / long以及它们的包装类(由于舍入原因不支持double和float),null是有效的 |
@DecimalMin | 必须是一个大于或者大于等于设定值的数字,其他同@DecimalMax |
@Digits | 设定可接受范围内的数字,必须指定integer(整数位数)和fraction(小数位数),支持BigDecimal、BigInteger、CharSequence、byte / short / int / long以及它们的包装类,null是有效的 |
必须是一个正确格式的邮箱,可以使用regexp指定正则表达式(默认是任意字符串),可以使用flags指定正则表达式的选项,null是有效的 | |
@Future | 必须是一个未来的瞬间、日期或时间(Now是虚拟机默认的当前时区的时间),支持java.util.Date、java.util.Calendar、java.time.Instant、java.time.LocalDate、java.time.LocalDateTime、java.time.LocalTime、java.time.MonthDay、java.time.OffsetDateTime、java.time.OffsetTime、java.time.Year、java.time.YearMonth、java.time.ZonedDateTime、java.time.chrono.HijrahDate、java.time.chrono.JapaneseDate、java.time.chrono.MinguoDate、java.time.chrono.ThaiBuddhistDate,null是有效的 |
@FutureOrPresent | 必须是现在或未来的瞬间、日期或时间,其他同@Future |
@Max | 必须是小于等于指定值的数字,value是long类型,支持BigDecimal、BigInteger、byte / short / int / long以及它们的包装类(不支持float和double),null是有效的 |
@Min | 必须是大于等于指定值的数字,其他同@Max |
@Negative | 必须是一个严格的负数,0为无效值,支持BigDecimal、BigInteger、byte / short / int / long / float / double以及它们的包装类,null是有效的 |
@NegativeOrZero | 必须是负数或者0,其他同@Negative |
@NotBlank | 不能为null,并且至少包含一个非空白字符,接受CharSequence |
@NotEmpty | 不能为null或空(集合),支持CharSequence(字符序列长度)、Collection(集合size)、Map(map size)、Array(数组长度) |
@NotNull | 不能为null,支持所有类型 |
@Null | 必须为null,支持所有类型 |
@Past | 必须是一个过去的瞬间、日期或时间,其他同@Future |
@PastOrPresent | 必须是现在或过去的瞬间、日期或时间,其他同@Future |
@Pattern | 必须符合指定的正则表达式,必须使用regexp参数指定正则表达式 |
@Positive | 必须是一个严格的正数,0为无效值,其他同@Negative |
@PositiveOrZero | 必须是正数或者0,其他同@Negative |
@Size | 指定元素大小必须在指定范围内(包括边界值),使用min指定下边界(默认0),使用max指定上边界(默认Integer.MAX_VALUE),支持CharSequence、Collection、Map、Array,null是有效的 |
在controller类 添加@Validated标签 在方法的参数前加验证标签 同一个参数可以添加多个标签; package testspringboot.test6paramvalidation; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Test6Main { public static void main(String[] args) { SpringApplication.run(Test6Main.class, args); } } //controller类: package testspringboot.test6paramvalidation; import java.util.list; import java.util.stream.Collectors; import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test6") @Validated public class Test6Controller { @RequestMapping("/a") public String a(@NotNull(message = "参数s不能为null") String s, @Min(8) @Max(value = 18) long a) { System.out.println(s); System.out.println(a); return String.format("s:%s a:%d", s, a); } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。