我正在使用REST-service
处理Spring 4
。我想验证过滤器链中的实体。我无法在@Validate
中使用标准@Controller
注释,因为请求的必需URI仅在过滤器级别指定(我扩展了UsernamePasswordAuthenticationFilter以进行进一步的自定义)。
请告诉我如何在过滤器内验证此类实体:
public class UserEntity {
@NotNull
@Email
private String username;
@NotNull
private String password;
//getters and setters
}
因此我希望能够从活页夹中检索错误。
由于
答案 0 :(得分:0)
我通过在LocalValidatorFactoryBean
类中声明@Configuration
来解决问题,然后在过滤级别使用它。
...
@Autowired
private Validator validator;
...
DataBinder binder = new DataBinder(entityWhichNeedToBeValidated);
binder.setValidator(validator);
binder.validate();
BindingResult result = binder.getBindingResult();
...
在此之后,我会根据需要获得有关已验证字段和完整程序流的完整信息。
答案 1 :(得分:0)
我认为这会给你一些关于使用spring验证实现rest调用的提示。 http://spring.io/blog/2012/08/29/integrating-spring-mvc-with-jquery-for-validation-rules