javax.validation - BindingResult

1.书写接收参数参数对象,使用 @NotBlank,@NotNull 注解
import javax.validation.constraints.NotBlank;import javax.validation.constraints.NotNull;

@Datapublic class AbcParam{

    @NotBlank(message = "课程名称不能为")    private String title;

    @NotNull(message = "价格不能为空")    private Integer price;}

2.书写 Controller 使用 @Valid 启用参数校验,并使用 参数 BindingResult (作用:使用该参数时,前端参数依然能被注入到abcParam,参数验证失败也不会抛出400,bindingResult 参数包含验证失败信息)
import javax.validation.Valid;import org.springframework.validation.BindingResult;
@RestController@RequestMapping(value = "/abc")public class AbcController {
  @PostMapping(value = "/save")  public Object save(@RequestBody @Valid AbcParam abcParam, BindingResult bindingResult){

     boolean hasErrors = bindingResult.hasErrors();     if(hasErrors){        List<String> errorMsgList = new ArrayList<String>();        List<FieldError> errors =  bindingResult.getFieldErrors();        for (FieldError error:errors) {           String errorMsg =  error.getDefaultMessage();           errorMsgList.add(errorMsg);        }      return errorMsgList;     }     return abcParam;  }}
3.测试,会进入controller



原文地址:https://www.cnblogs.com/abdusalam10/p/11888542.html

时间: 2024-10-02 20:17:44

javax.validation - BindingResult的相关文章

spring boot中使用javax.validation以及org.hibernate.validator校验入参

这里springboot用的版本是:<version>2.1.1.RELEASE</version> 自带了hibernate.validator,所以不用添加额外依赖 1.创建个对应前台form的测试vo,并添加字段校验注解 import java.math.BigDecimal; import java.util.Date; import java.util.List; import javax.validation.constraints.AssertTrue; import

javax.validation.ConstraintViolationException

javax.validation.ConstraintViolationException ... 71 moreCaused by: javax.validation.ConstraintViolationException: validation failed forclasses [cn.com.softvan.travelb2b.domain.TravelLine] during persist time for groups [javax.validation.groups.Defau

javax.validation 自定义校验器

import java.util.Iterator; import java.util.List; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; import org.apache.commons.lang3.StringUtils; public class ListStringPatternValidator implements Constra

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint

使用hibernate validator出现上面的错误, 需要 注意 @NotNull 和 @NotEmpty  和@NotBlank 区别 @NotEmpty 用在集合类上面@NotBlank 用在String上面@NotNull    用在基本类型上 如果在基本类型上面用NotEmpty或者NotBlank 会出现上面的错 http://blog.csdn.net/dracotianlong/article/details/23181729 javax.validation.Unexpec

[javax.validation]验证

package main; import java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; import javax.validation.constraints.Max; import javax.va

Exception in thread &quot;main&quot; javax.validation.ValidationException: Unable to find a default provider

Exception in thread "main" javax.validation.ValidationException: Unable to find a default provider 在使用Spring MVC+Mybatis整合的时候出现上面的问题 要将下面三个包加入项目的BuildPath中才行:    hibernate-validator-4.2.0.Final.jar    validation-api-1.0.0.GA.jar    slf4j-api-1.6

javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found

1 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0': Invocation of init method failed; nested exception is javax.validation.ValidationExc

javax.validation.UnexpectedTypeException: No validator could be found for type: java.lang.Integer

[java] view plaincopyprint? Apr 8, 2014 2:13:57 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet springmvc threw exception javax.validation.UnexpectedTypeException: No validator could be found for type: ja

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 解决方法

在使用hibernate validator进行参数有效性校验的时候,我们有时候会遇到javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint ,比如,在我们的应用中,使用了自定义注解Dict对枚举进行有效性校验,因为定义的Dict应用于String类型,例如: package com.yidoo.base.metadata.validate; import jav