对与控制器方法中的集合绑定的RequestBody参数进行Spring验证 [英] Spring validation for RequestBody parameters bound to collections in Controller methods

查看:57
本文介绍了对与控制器方法中的集合绑定的RequestBody参数进行Spring验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

实体:

package org.ibp.soq;

public class MyEntity {

    private String field1;
    private String field2;

    //..getters and setters

}

实体的验证者:

package org.ibp.soq;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

@Component
public class MyEntityValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return MyEntity.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        MyEntity myEntity = (MyEntity) target;
        // Logic to validate my entity
        System.out.print(myEntity);
    }

}

具有批量PUT方法的REST控制器:

package org.ibp.soq;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/myEntity")
public class MyEntityRestResource {

    @Autowired
    private MyEntityValidator myEntityValidator;

    @InitBinder
    protected void initBinder(final WebDataBinder binder) {
        binder.addValidators(this.myEntityValidator);
    }

    @RequestMapping(method = RequestMethod.PUT)
    public void bulkCreate(@RequestBody @Valid List<MyEntity> myEntities) {
        // Logic to bulk create entities here.
        System.out.print(myEntities);
    }
}

当我通过以下请求正文对此资源进行PUT请求时:

[
  {
    "field1": "AA",
    "field2": "11"
  },

  {
    "field1": "BB",
    "field2": "22"
  }
]

我得到的错误是:

"Invalid target for Validator [org.ibp.soq.MyEntityValidator@4eab617e]: [org.ibp.soq.MyEntity@21cebf1c, org.ibp.soq.MyEntity@c64d89b]"

我可以理解,这是因为MyEntityValidator支持"单个MyEntity验证,而不是对ArrayList<MyEntity>的验证.

如果我在请求正文中具有单个MyEntity对象,并且具有带有@RequestBody @Valid MyEntity myEntity参数的相应控制器方法,则

MyEntityValidator可以完美地工作.

如何扩展我使用的验证器设置以支持对MyEntity的集合的验证?

解决方案

解决方案是为Collection创建自定义Validator,并为@ControllerAdvice创建一个将Validator注册到WebDataBinders@ControllerAdvice. >

验证者:

 import java.util.Collection;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

/**
 * Spring {@link Validator} that iterates over the elements of a 
 * {@link Collection} and run the validation process for each of them
 * individually.
 *   
 * @author DISID CORPORATION S.L. (www.disid.com)
 */
public class CollectionValidator implements Validator {

  private final Validator validator;

  public CollectionValidator(LocalValidatorFactoryBean validatorFactory) {
    this.validator = validatorFactory;
  }

  @Override
  public boolean supports(Class<?> clazz) {
    return Collection.class.isAssignableFrom(clazz);
  }

  /**
   * Validate each element inside the supplied {@link Collection}.
   * 
   * The supplied errors instance is used to report the validation errors.
   * 
   * @param target the collection that is to be validated
   * @param errors contextual state about the validation process
   */
  @Override
  @SuppressWarnings("rawtypes")
  public void validate(Object target, Errors errors) {
    Collection collection = (Collection) target;
    for (Object object : collection) {
      ValidationUtils.invokeValidator(validator, object, errors);
    }
  }
}
 

ControllerAdvice:

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

/**
 * Controller advice that adds the {@link CollectionValidator} to the 
 * {@link WebDataBinder}.
 * 
 * @author DISID CORPORATION S.L. (www.disid.com)
 */
@ControllerAdvice
public class ValidatorAdvice {

  @Autowired
  protected LocalValidatorFactoryBean validator;


  /**
   * Adds the {@link CollectionValidator} to the supplied 
   * {@link WebDataBinder}
   * 
   * @param binder web data binder.
   */
  @InitBinder
  public void initBinder(WebDataBinder binder) {
    binder.addValidators(new CollectionValidator(validator));
  }
}
 

I have

An Entity:

package org.ibp.soq;

public class MyEntity {

    private String field1;
    private String field2;

    //..getters and setters

}

Validator for the entity:

package org.ibp.soq;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

@Component
public class MyEntityValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return MyEntity.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        MyEntity myEntity = (MyEntity) target;
        // Logic to validate my entity
        System.out.print(myEntity);
    }

}

and

The REST controller with bulk PUT method:

package org.ibp.soq;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/myEntity")
public class MyEntityRestResource {

    @Autowired
    private MyEntityValidator myEntityValidator;

    @InitBinder
    protected void initBinder(final WebDataBinder binder) {
        binder.addValidators(this.myEntityValidator);
    }

    @RequestMapping(method = RequestMethod.PUT)
    public void bulkCreate(@RequestBody @Valid List<MyEntity> myEntities) {
        // Logic to bulk create entities here.
        System.out.print(myEntities);
    }
}

When I make a PUT request to this resource with following request body:

[
  {
    "field1": "AA",
    "field2": "11"
  },

  {
    "field1": "BB",
    "field2": "22"
  }
]

The error I get is:

"Invalid target for Validator [org.ibp.soq.MyEntityValidator@4eab617e]: [org.ibp.soq.MyEntity@21cebf1c, org.ibp.soq.MyEntity@c64d89b]"

I can understand that this is because MyEntityValidator "supports" single MyEntity validation, not validation for ArrayList<MyEntity>.

MyEntityValidator works perfectly if I have single MyEntity object in request body and a corresponding controller method with @RequestBody @Valid MyEntity myEntity parameter.

How can the validator setup I have used, be extended for supporting validation of collection of MyEntity's ?

解决方案

The solution is to create a custom Validator for Collection and a @ControllerAdvice that registers that Validator in the WebDataBinders.

Validator:

import java.util.Collection;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

/**
 * Spring {@link Validator} that iterates over the elements of a 
 * {@link Collection} and run the validation process for each of them
 * individually.
 *   
 * @author DISID CORPORATION S.L. (www.disid.com)
 */
public class CollectionValidator implements Validator {

  private final Validator validator;

  public CollectionValidator(LocalValidatorFactoryBean validatorFactory) {
    this.validator = validatorFactory;
  }

  @Override
  public boolean supports(Class<?> clazz) {
    return Collection.class.isAssignableFrom(clazz);
  }

  /**
   * Validate each element inside the supplied {@link Collection}.
   * 
   * The supplied errors instance is used to report the validation errors.
   * 
   * @param target the collection that is to be validated
   * @param errors contextual state about the validation process
   */
  @Override
  @SuppressWarnings("rawtypes")
  public void validate(Object target, Errors errors) {
    Collection collection = (Collection) target;
    for (Object object : collection) {
      ValidationUtils.invokeValidator(validator, object, errors);
    }
  }
}

ControllerAdvice:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

/**
 * Controller advice that adds the {@link CollectionValidator} to the 
 * {@link WebDataBinder}.
 * 
 * @author DISID CORPORATION S.L. (www.disid.com)
 */
@ControllerAdvice
public class ValidatorAdvice {

  @Autowired
  protected LocalValidatorFactoryBean validator;


  /**
   * Adds the {@link CollectionValidator} to the supplied 
   * {@link WebDataBinder}
   * 
   * @param binder web data binder.
   */
  @InitBinder
  public void initBinder(WebDataBinder binder) {
    binder.addValidators(new CollectionValidator(validator));
  }
}

这篇关于对与控制器方法中的集合绑定的RequestBody参数进行Spring验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆