Spring支持两种不同的验证方法:Spring验证和JSR-303 bean验证.两者都可以通过定义一个Spring验证器来使用,该验证器委托给其他委托者,包括bean验证器.到现在为止还挺好.
但是当注释实际请求验证的方法时,这是另一个故事.我可以像这样注释
@RequestMapping(value = "/object", method = RequestMethod.POST)
public @ResponseBody TestObject create(@Valid @RequestBody TestObject obj, BindingResult result) {
Run Code Online (Sandbox Code Playgroud)
或者像这样
@RequestMapping(value = "/object", method = RequestMethod.POST)
public @ResponseBody TestObject create(@Validated @RequestBody TestObject obj, BindingResult result) {
Run Code Online (Sandbox Code Playgroud)
这里,@ Valid是javax.validation.Valid,而@Validated是org.springframework.validation.annotation.Validated.后者的文档说
JSR-303的变体有效,支持验证组的规范.设计方便使用Spring的JSR-303支持,但不支持JSR-303特定.
这没有多大帮助,因为它没有确切地说明它是如何不同的.如果有的话.两者似乎都对我很好.
Validator尝试使用JSR-303(hibernate-validator)验证模型时,我在注入spring应用程序bean时遇到问题
我的主要配置类是:
@EnableAutoConfiguration
@EnableWebMvc // <---
@EnableJpaRepositories("com.example")
@EntityScan("com.example")
public class MainConfiguration {
Run Code Online (Sandbox Code Playgroud)
根据javadocs:
/**
* Provide a custom {@link Validator} instead of the one created by default.
* The default implementation, assuming JSR-303 is on the classpath, is:
* {@link org.springframework.validation.beanvalidation.LocalValidatorFactoryBean}.
* Leave the return value as {@code null} to keep the default.
*/
Validator getValidator();
Run Code Online (Sandbox Code Playgroud)
Hibernate-validator在类路径上.我正在尝试将其注入存储库:
@Repository
public class UserRepositoryImpl implements UserRepositoryCustom {
@Autowired
private Validator validator;
Run Code Online (Sandbox Code Playgroud)
抛出异常:
No qualifying bean of type [javax.validation.Validator] found for dependency:
Run Code Online (Sandbox Code Playgroud)
更新: …
我通过剖析示例应用程序然后在这里和那里添加代码来测试我在解剖过程中开发的理论,从而自学Spring.我测试一些我添加到Spring应用程序的代码时收到以下错误消息:
An Errors/BindingResult argument is expected to be declared immediately after the
model attribute, the @RequestBody or the @RequestPart arguments to which they apply
Run Code Online (Sandbox Code Playgroud)
错误消息引用的方法是:
@RequestMapping(value = "/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(Integer typeID, BindingResult result, Map<String, Object> model) {
// find owners of a specific type of pet
typeID = 1;//this is just a placeholder
Collection<Owner> results = this.clinicService.findOwnerByPetType(typeID);
model.put("selections", results);
return "owners/catowners";
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在Web浏览器中加载/ catowners url模式时,会触发此错误消息.我已经回顾了这个页面和这个帖子,但解释似乎并不清楚.
任何人都可以告诉我如何解决这个错误,并解释它是什么意思?
编辑:
基于Biju Kunjummen的回复,我将语法更改为以下内容:
@RequestMapping(value = "/catowners", …Run Code Online (Sandbox Code Playgroud) Spring 3.2引入了@ControllerAdvice用于处理Spring MVC应用程序中的异常的注释.但在此版本之前,Spring已经@ExceptionHandler或正在HandlerExceptionResolver处理Spring MVC应用程序中的异常.那么为什么Spring 3.2引入了@ControllerAdvice用于处理异常的注释?我坚信,春3.2引入了@ControllerAdvice注释,以解决限制 @ExceptionHandler或HandlerExceptionResolver或使例外处理更强.
任何人都可以解释@ControllerAdvice超过@ExceptionHandler或HandlerExceptionResolver处理异常的优点吗?