@Valid 不适用于弹簧座控制器 [英] @Valid not working for spring rest controller
本文介绍了@Valid 不适用于弹簧座控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已将rest终结点方法定义为:
I have defined a rest endpoint method as:
@GetMapping("/get")
public ResponseEntity getObject(@Valid MyObject myObject){....}
这会将请求参数映射到MyObject.
This maps request parameters to MyObject.
MyObject定义为(带有lombok,javax.validation批注):
MyObject is defined as(with lombok, javax.validation annotations):
@Value
@AllArgsConstructor
public class MyObject {
@Min(-180) @Max(180)
private double x;
@Min(-90) @Max(90)
private double y;
}
但是验证不起作用.即使值超出规定范围,请求也不会抛出错误并且运行顺利.
But validations are not working. Even with values out of prescribed range, request doesn't throw error and goes well.
推荐答案
使用 org.springframework.validation.annotation.Validated
这篇关于@Valid 不适用于弹簧座控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文