[英]Spring mvc controller jsr 303 basic list validation
I have a simple MVC controller that takes a list of emails, and will then send a message to those emails. 我有一个简单的MVC控制器,它接收电子邮件列表,然后将消息发送到这些电子邮件。
{"emailAddresses" : []}
@RequestMapping(value = "/{id}/share", method = RequestMethod.POST)
@ResponseBody
public void shareThing(@PathVariable(value = "id") final String id, @Valid @NotEmpty @RequestBody final List<String> emailAddresses)
Basically, I want to use jsr-303 so that if the client posts a request with no emails, it fails. 基本上,我想使用jsr-303,以便如果客户端发布不带电子邮件的请求,则失败。 Preferably with a 401. 最好带有401。
Should the above code work? 上面的代码应该工作吗? Or what do I need to do? 还是我该怎么办? Here's the setup. 这是设置。 Hibernate validator is on the classpath, so it should be picking up. Hibernate验证程序位于类路径上,因此应该可以使用。 However if I post an empty json array, it goes straight into the method with an empty array list as the bound parameter. 但是,如果我发布一个空的json数组,它将直接进入一个以空数组列表作为绑定参数的方法。
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.package.thing"})
public class WeConfiguration extends WebMvcConfigurerAdapter
{"emailAddresses" : []}
@RequestMapping(value = "/{id}/share", method = RequestMethod.POST)
@ResponseBody
public void shareThing(@PathVariable(value = "id") final String id, @Valid @NotEmpty @RequestBody final List<String> emailAddresses, BindingResult bindingResult)
{
System.out.println("Has Errors? " + bindingResult.hasErrors();
}
Has Errors? 有错误吗? false 假
public class EmailListDTO {
@NotEmpty
private List<String> emailAddresses;
public List<String> getEmailAddresses() {
return emailAddresses;
}
public void setEmailAddresses(List<String> emailAddresses) {
this.emailAddresses = emailAddresses;
}
} }
@RequestMapping(value = "/{id}/share", method = RequestMethod.POST)
@ResponseBody
public void shareThing(@PathVariable(value = "id") final String id, @Valid @RequestBody EmailListDTO emailListDTO, BindingResult bindingResult)
{
System.out.println("Has Errors? " + bindingResult.hasErrors();
}
{"emailAddresses" : []}
Has Errors? 有错误吗? true 真正
@Valid
validates if the parameter class type itself has javax.validation
annotations on fields. @Valid
验证参数类类型本身是否在字段上具有javax.validation
批注。 So it would use the Validator
to scan the List
class for validation annotations, which is not what you want. 因此,它将使用Validator
扫描List
类以获取验证注释,这不是您想要的。
You should wrap your List<String>
into a DTO and change your parameter to that 您应该将List<String>
包装到DTO中,并将参数更改为
public class MyListDTO {
@NotEmpty
private List<String> emailAddresses;
... //getters and setters
}
@Valid
doesn't just make it fail, it binds the error to a BindingResult
object. @Valid
不仅使它失败, BindingResult
错误绑定到BindingResult
对象。 If you add add a BindingResult
parameter to your handler method (it must be next to your @Valid
parameter) 如果添加,请在处理程序方法中添加BindingResult
参数(该参数必须位于@Valid
参数旁边)
public void shareThing(@PathVariable(value = "id") final String id, @Valid @RequestBody final MyListDTO myDTO, BindingResult errors )
you can then checks the BindingResult
for errors and choose what to do. 然后,您可以检查BindingResult
是否有错误,然后选择要执行的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.