Spring 多部分文件的自定义验证程序
我正在尝试使用Spring 多部分文件的自定义验证程序,spring,spring-mvc,spring-boot,spring-validator,Spring,Spring Mvc,Spring Boot,Spring Validator,我正在尝试使用Spring验证器构建一个自定义验证器来检查多部分文件,但我面临这个问题: An Errors/BindingResult argument is expected to be declared immediately after the model attribute, the @RequestBody or the @RequestPart arguments to which they apply: public org.springframework.web.contex
Spring
验证器构建一个自定义验证器来检查多部分文件
,但我面临这个问题:
An Errors/BindingResult argument is expected to be declared immediately after the model attribute, the @RequestBody or the @RequestPart arguments to which they apply: public org.springframework.web.context.request.async.DeferredResult br.com.mobtrack.api.resource.CityResource.uploadImage(org.springframework.web.multipart.MultipartFile,org.springframework.validation.BindingResult)
这是我的验证器类:
@Component
public class MultipartFileValidator implements Validator{
@Override
public boolean supports(Class<?> aClass) {
return MultipartFile.class.isAssignableFrom(aClass);
}
@Override
public void validate(Object o, Errors errors) {
MultipartFile file = (MultipartFile) o;
if (file == null || file.isEmpty()){
errors.reject("image","send a valid image. );
}
}
}
BindingResult参数应在模型属性、@RequestBody或@RequestPart参数之后立即声明
它与验证器无关。它似乎与控制器在一起
它似乎在告诉你这个问题。您需要使用@RequestBody或@RequestPart注释来告诉它在哪里查找要绑定的数据。尝试了如何使用它们。它们不在提供的示例中。它们是否紧跟在消息中描述的模型之后?你那样做的时候发生了什么?相同的错误错误是否发生了更改?仅仅说“不起作用”或“出错”并不能给任何人带来任何可以合作的东西。
@PostMapping("/image")
public DeferredResult<String> uploadImage(@Valid MultipartFile image, BindingResult result) {
if (result.hasErrors()) {
throw new ImageNotFoundException("test");
} else {
DeferredResult<String> deferredResult = new DeferredResult<>();
Thread thread = new Thread(new ImageStorageRunnable(image, deferredResult, imageStorage));
thread.start();
return deferredResult;
}
}