【发布时间】:2018-03-05 14:11:53
【问题描述】:
我正在寻找类似 JSR-303 Validation Groups 的东西(bean 验证,当您在控制器中使用 @Validated(GroupName.class) 标记方法参数并在需要的请求类字段中指定组时),但它应该决定如何根据请求字段之一在运行时进行验证。
例如,如果我们有这样的控制器类
@Controller
public class MyController {
@Autowired
private MyService myService;
@ResponseBody
@RequestMapping(value = "/path", method = RequestMethod.PUT)
public ResponseVo storeDetail(/*maybe some annotation here*/ DetailRequestVo requestVo) {
return myService.storeDetail(requestVo);
}
class DetailRequestVo {
String type;
Long weight;
Long radius;
}
}
我们希望根据 type 字段值进行验证:如果 type = "wheel" 则应显示半径和 weight 字段,如果 type = "engine" 则应仅显示 weight 字段。
Spring(自 3.2.17 起)是否提供 API 以更具声明性的方式实现这些规则? org.springframework.validation.Validator 在这里看起来不是一个选项,因为它的方法 boolean supports(Class<?> clazz) 基于类信息而不是实例信息来决定。
提前致谢
【问题讨论】:
标签: java spring validation http-request-parameters