这是我的控制器。它接受带有两个字段multipart/form-data
和form
的file
请求。 form
字段是一个MyObject
,file
字段是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)
从不被调用。除此之外,我的Controller
运转良好,并努力地将对象保存到repo
。这可能是什么问题?我已经读过类似的问题,常见的错误是不在
@InitBinder
批注内使用适当的参数,或者将@InitBinder
方法设置为private
而不是public
,但这两种情况均不适用于我的情况。这种丑陋的解决方法可以实现预期的功能,但它与Spring并不一样。我在
FileValidator
方法内手动调用了Controller.createMyObject
,而不是让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
设为单个对象参数(并相应地更改验证器)以测试其是否以此方式工作。