我有一个类在某些字段上使用hibernate的验证注释(例如@NotNull和@Size(min = 4,max = 50)等)
public class MyClass {
Long id;
@NotEmpty
@Size(min = 4,max = 50)
String machineName;
@NotEmpty
@Size(min = 4,max = 50)
String humanName;
// Getters,setters,etc…
}
我还有一个用作JSON API的自定义控制器,以及调用API方法时创建MyClass对象的JSON解串器.在我的自定义控制器中,我有一个方法来创建一个新的对象:
@RequestMapping(method = RequestMethod.POST)
public long createMyObject(@RequestBody @Valid MyClass newObj) {
// Create the object in the database
return newObj.getId();
}
以及更新现有对象的另一种方法
@RequestMapping(method = RequestMethod.PUT)
public void updateMyObject(@RequestBody MyClass updatedObj) {
MyClass existingObj = // Get existing obj from DB by updatedObj.getId();
// Do some secondary validation,such as making sure that a specific
// field remains unchanged compared to the existing instance
if (existingObj.getMachineName() != null &&
!existingObj.getMachineName().equals(updatedObj.getMachineName())) {
throw new CannotChangeMachineNameException();
}
else {
updatedObj.setMachineName(existingObj.getMachineName());
}
// [HERE IS WHERE I WANT THE MAGIC TO HAPPEN]
// Save updatedObj to the database
}
虽然我可以在createMyObject中使用@Valid,但我不能在updateMyObject中使用它,因为我们的API实现需要machineName保持不变 – 用户可以使用JSON对象调用API,该对象完全排除machineName,或者使用与数据库.*
在将更新的对象保存到数据库之前,我想调用具有@Valid注释的相同验证器将被调用.如何找到这个验证器并使用它?
解决方法
没有什么说你只需要在控制器方法中使用@Valid.为什么不使用一个验证方法来接受您注释为@Valid的参数,然后只返回相同的参数.
喜欢这个:
public Book validateBook(@Valid Book book) {
return book;
}
看起来一个替代方案是使用Hibernate的验证包. Here’s it’s documentation.
基本上,您从ValidationFactory获得验证器,然后使用验证器,如下所示:
@Test
public void manufacturerIsNull() {
Car car = new Car(null,"DD-AB-123",4);
Set<ConstraintViolation<Car>> constraintViolations =
validator.validate(car);
assertEquals(1,constraintViolations.size());
assertEquals("may not be null",constraintViolations.iterator().next().getMessage());
}
