这是我的控制器.它接受带有两个字段(表单和文件)的multipart / form-data请求.表单字段是
MyObject,文件字段是MultipartFile.这两个变量都用@Valid注释,因此,我希望
Spring调用每个相应字段的Validator类.但是,这仅适用于MyObject,而不适用于MultipartFile.
@RequestMapping("/api")
@RestController
public class Controller {
private MyObjectRepository repo;
private MyObjectValidator myObjectValidator;
private FileValidator fileValidator;
@Autowired
public myObjectController(MyObjectRepository repo, MyObjectValidator myObjectValidator,
FileValidator fileValidator) {
this.repo = repo;
this.myObjectValidator = myObjectValidator;
this.fileValidator = fileValidator;
}
@InitBinder("form")
public void initMyObjectBinder(WebDataBinder binder) {
binder.setValidator(this.myObjectValidator);
}
@InitBinder("file")
public void initFileBinder(WebDataBinder binder) {
binder.setValidator(this.fileValidator);
}
@PostMapping("myObject")
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public MyObject createMyObject(@RequestPart("form") @Valid MyObject myObject,
@RequestPart("file") @Valid MultipartFile... file) {
return repo.save(myObject);
}
}
我的MyObjectValidator被触发,但我的FileValidator没有被触发.这两个类都实现了Spring Validator接口.调用MyObjectValidator.supports(Class<?> aClass),而从不调用FileValidator.supports(Class<?> aClass).除此之外,我的控制器运行正常,并努力将对象保存到我的仓库.
这可能是什么问题?我读过类似的问题,常见的错误是不在@InitBinder注释中使用适当的参数,或者将@InitBinder方法设置为private而不是public,但这两种方法都不适用于我的情况.
这个丑陋的解决方法实现了它应该做的,但它不像Spring那样.我在Controller.createMyObject方法中手动调用我的FileValidator,而不是让Spring通过@Valid注释自动调用它.
@PostMapping("myObject")
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public MyObject createMyObject(@RequestPart("form") @Valid MyObject myObject,
@RequestPart("file") @Valid MultipartFile... file) {
if (fileValidator.supports(file.getClass())) {
Errors errors = new BeanPropertyBindingResult(file, "Uploaded file.");
fileValidator.validate(file,errors);
if (errors.hasErrors()) {
throw new BadRequestException();
}
}
return repo.save(myObject);
}
编辑:我已根据要求包含我的Validator类.
import org.springframework.validation.Validator;
public abstract class AbstractValidator implements Validator {
// One shared method here.
}
public class FileValidator extends AbstractValidator {
public boolean supports(Class<?> aClass) { // This method is never triggered.
boolean isSingleFile = MultipartFile.class.isAssignableFrom(aClass); // This line has a breakpoint, it is never triggered in the debugger.
boolean isFileArray = aClass.equals(MultipartFile[].class);
return (isSingleFile || isFileArray);
}
public void validate(Object o, Errors e) {
//Several validation methods go here.
}
public class MyObjectValidator extends AbstractValidator {
public boolean supports(Class<?> aClass) { // This method is triggered.
return (MyObject.class.equals(aClass)); // This line has a breakpoint, and it is always triggered in the debugger.
}
public void validate(Object o, Errors e) {
// Several validation methods go here.
}
编辑:我对我的代码进行了一些更改,如NiVeR建议,删除varargs参数并相应地更改我的FileValidator.supports(Class<?> aClass),但行为仍然相同.
在Controller.java中:
@PostMapping("myObject")
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public MyObject createMyObject(@RequestPart("form") @Valid MyObject myObject, @RequestPart("file") @Valid MultipartFile file) {
return repo.save(myObject);
}
在FileValidator.java中:
public boolean supports(Class<?> aClass) {
return MultipartFile.class.isAssignableFrom(aClass);
}
最佳答案 我相信这个问题与可变参数Multipart …参数有关.在验证器的supports方法中,您正在检查Multipart数组,但我怀疑这不是正确的方法.就像试验一样,我会将Multipart作为单个对象参数(并相应地更改验证器)以测试它是否以这种方式工作.