springboot validate,springboot @validated
目录
简介依赖用法1:不分组代码测试用法2:分组代码测试
简介
说明
本文用示例说明跳羚的@已验证的用法。
依赖
依赖关系groupIdorg.springframework.boot/groupId artifact id spring-boot-starter-验证/artifact id/依赖关系它里边依赖了冬眠验证程序,就是下边这个
依赖性groupIdorg.hibernate.validator/groupId人工冬眠-验证器/artifactId/依赖性
用法1:不分组
跟上边" @有效实例"的结果是一样的,只是把入参处的@有效改为@已验证.
代码
Controller
包com。举例。演示。已验证。不带_组。控制器;导入com。举例。演示。已验证。不带_组。实体。用户;导入io。昂首阔步。注释。API导入io。昂首阔步。注释。API操作;导入org。spring框架。验证。绑定结果;导入组织。spring框架。验证。对象错误;导入org。spring框架。验证。注释。已验证;导入org。spring框架。网络。绑定。注释。获取映射;导入org。spring框架。网络。绑定。注释。请求映射;导入org。spring框架。网络。绑定。注释。休息控制器;导入javax。验证。有效;导入Java。util。列表;@Api(tags=不分组)@ rest控制器@请求映射( validatedWithoutGroup )公共类validatedwithouttgroupcontroller { @ API操作(正常用法)@ get mapping( normal )public User normal(@ Validated User User){ return User;} @ApiOperation(获得绑定结果)@ get映射(绑定结果)公共用户binding result(@已验证的用户用户,绑定结果绑定结果){ if(绑定结果。has errors()){ ListObjectError list=绑定结果。getallerrors();for(object error object error : list){ system。出去。println(ObjectError。getdefaultmessage());}//系统。出去。println(绑定结果。getfield错误().getDefaultMessage());}返回用户;} }Entity
用户类
:java;">package com.example.demo.validated.without_group.entity; import lombok.Data; import javax.validation.Valid;import javax.validation.constraints.NotBlank;import javax.validation.constraints.NotEmpty;import javax.validation.constraints.NotNull;import java.util.List; @Datapublic class User { @NotBlank(message = "名字不能为空") private String name; private Integer age; @NotBlank(message = "密码不能为空") private String password; @NotEmpty(message = "分数不能为空") private List<Integer> scoreArray; @Valid @NotNull(message = "账户不能为null") private Account account;}Account类
package com.example.demo.validated.without_group.entity; import lombok.Data; import javax.validation.constraints.NotBlank; @Datapublic class Account { @NotBlank(message = "电话号码不能为空") private String phoneNumber; private String[] emails;}
测试
测试1:缺少字段
postman访问:http://localhost:8080/validatedWithoutGroup/normal
postman结果:
后端结果:
2021-12-22 16:12:26.549 WARN 79176 --- [nio-8080-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 3 errorsField error in object 'user' on field 'scoreArray': rejected value [null]; codes [NotEmpty.user.scoreArray,NotEmpty.scoreArray,NotEmpty.java.util.List,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.scoreArray,scoreArray]; arguments []; default message [scoreArray]]; default message [分数不能为空]Field error in object 'user' on field 'password': rejected value [null]; codes [NotBlank.user.password,NotBlank.password,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.password,password]; arguments []; default message [password]]; default message [密码不能为空]Field error in object 'user' on field 'account': rejected value [null]; codes [NotNull.user.account,NotNull.account,NotNull.com.example.demo.validated.without_group.entity.Account,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.account,account]; arguments []; default message [account]]; default message [账户不能为null]]
测试2:不缺少字段
postman访问:http://localhost:8080/validatedWithoutGroup/normal
postman结果:
测试3: 获得BindingResult
postman访问:http://localhost:8080/validatedWithoutGroup/bindingResult
postman结果:
后端结果:
分数不能为空密码不能为空账户不能为null
用法2:分组
代码
Controller
package com.example.demo.validated.with_group.controller; import com.example.demo.validated.with_group.entity.User;import com.example.demo.validated.with_group.validatation.IGroupA;import com.example.demo.validated.with_group.validatation.IGroupAll;import com.example.demo.validated.with_group.validatation.IGroupB;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import org.springframework.validation.BindingResult;import org.springframework.validation.ObjectError;import org.springframework.validation.annotation.Validated;import org.springframework.web.bind.annotation.*; import java.util.List; @Api(tags = "分组")@RestController@RequestMapping("validatedWithGroup")public class ValidatedWithGroupController { @ApiOperation("使用组:GroupA") @GetMapping("groupA") public User groupA(@Validated({IGroupA.class}) User user) { return user; } @ApiOperation("使用组:GroupB") @GetMapping("groupB") public User groupB(@Validated({IGroupB.class}) User user) { return user; } @ApiOperation("使用组:GroupA和GroupB") @GetMapping("groupAAndGroupB") public User groupAAndGroupB(@Validated({IGroupA.class, IGroupB.class}) User user) { return user; } @ApiOperation("使用组:GroupAll") @GetMapping("groupAll") public User groupAll(@Validated({IGroupAll.class}) User user) { return user; } }
Entity
User类
package com.example.demo.validated.with_group.entity; import com.example.demo.validated.with_group.validatation.IGroupA;import com.example.demo.validated.with_group.validatation.IGroupB;import lombok.Data; import javax.validation.Valid;import javax.validation.constraints.NotBlank;import javax.validation.constraints.NotEmpty;import javax.validation.constraints.NotNull;import java.util.List; @Datapublic class User { @NotBlank(message = "名字不能为空") private String name; @NotNull(message = "年龄不能为空", groups = {IGroupA.class}) private Integer age; @NotEmpty(message = "密码不能为空", groups = {IGroupB.class}) private String password; @NotEmpty(message = "分数不能为空", groups = {IGroupA.class, IGroupB.class}) private List<Integer> scoreArray; @Valid @NotNull(message = "账户不能为null") private Account account;}
Account类
package com.example.demo.validated.with_group.entity; import lombok.Data; import javax.validation.constraints.NotEmpty; @Datapublic class Account { @NotEmpty(message = "电话号码不能为空") private String phoneNumber; private String[] emails;}
Group
IGroupA接口
package com.example.demo.validated.with_group.validatation; public interface IGroupA {}
IGroupB接口
package com.example.demo.validated.with_group.validatation; public interface IGroupB {}
IGroupAll接口
package com.example.demo.validated.with_group.validatation; import javax.validation.GroupSequence;import javax.validation.groups.Default; @GroupSequence({Default.class, IGroupA.class, IGroupB.class})public interface IGroupAll {}
测试
测试1:无参数请求groupA
postman访问:http://localhost:8080/validatedWithGroup/groupA
postman结果:
后端结果:
2021-12-22 16:32:44.138 WARN 85532 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errorsField error in object 'user' on field 'scoreArray': rejected value [null]; codes [NotEmpty.user.scoreArray,NotEmpty.scoreArray,NotEmpty.java.util.List,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.scoreArray,scoreArray]; arguments []; default message [scoreArray]]; default message [分数不能为空]Field error in object 'user' on field 'age': rejected value [null]; codes [NotNull.user.age,NotNull.age,NotNull.java.lang.Integer,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.age,age]; arguments []; default message [age]]; default message [年龄不能为空]]
测试2:无参数请求groupB
postman访问:http://localhost:8080/validatedWithGroup/groupB
postman结果:
后端结果:
2021-12-22 16:33:15.773 WARN 85532 --- [nio-8080-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errorsField error in object 'user' on field 'scoreArray': rejected value [null]; codes [NotEmpty.user.scoreArray,NotEmpty.scoreArray,NotEmpty.java.util.List,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.scoreArray,scoreArray]; arguments []; default message [scoreArray]]; default message [分数不能为空]Field error in object 'user' on field 'password': rejected value [null]; codes [NotEmpty.user.password,NotEmpty.password,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.password,password]; arguments []; default message [password]]; default message [密码不能为空]]
测试3:无参数请求groupA和groupB
postman访问:http://localhost:8080/validatedWithGroup/groupAAndGroupB
postman结果:
后端结果:
2021-12-22 16:34:27.652 WARN 85532 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 3 errorsField error in object 'user' on field 'scoreArray': rejected value [null]; codes [NotEmpty.user.scoreArray,NotEmpty.scoreArray,NotEmpty.java.util.List,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.scoreArray,scoreArray]; arguments []; default message [scoreArray]]; default message [分数不能为空]Field error in object 'user' on field 'age': rejected value [null]; codes [NotNull.user.age,NotNull.age,NotNull.java.lang.Integer,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.age,age]; arguments []; default message [age]]; default message [年龄不能为空]Field error in object 'user' on field 'password': rejected value [null]; codes [NotEmpty.user.password,NotEmpty.password,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.password,password]; arguments []; default message [password]]; default message [密码不能为空]]
测试4:无参数请求groupAll
postman访问:http://localhost:8080/validatedWithGroup/groupAll
postman结果:
后端结果:
2021-12-22 16:36:54.095 WARN 91820 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errorsField error in object 'user' on field 'name': rejected value [null]; codes [NotBlank.user.name,NotBlank.name,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.name,name]; arguments []; default message [name]]; default message [名字不能为空]Field error in object 'user' on field 'account': rejected value [null]; codes [NotNull.user.account,NotNull.account,NotNull.com.example.demo.validated.with_group.entity.Account,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.account,account]; arguments []; default message [account]]; default message [账户不能为null]]
可以看到:走的校验逻辑是没有除了IGroupA和IGroupB注解的字段的逻辑。
以上就是SpringBoot参数校验之@Validated的使用详解的详细内容,更多关于SpringBoot参数校验@Validated的资料请关注盛行IT其它相关文章!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。