详解在Spring MVC中使用注解的方式校验RequestParams
概述
SpringMVC支持BeanValidation,通过这个验证技术,可以通过注解方式,很方便的对输入参数进行验证,之前使用的校验方式,都是基于Bean对象的,但是在@RequestParam中,没有Bean对象,这样使得校验无法进行,可以通过使用@Validated注解,使得校验可以进行。
校验bean对象
一般校验bean对象,为了可以自动的校验属性,可以通过两步解决:
一、声明对象
packagecom.github.yongzhizhan.draftbox.model; importjavax.validation.constraints.Size; /** *带验证的对象 *@authorzhanyongzhi */publicclassFoo{ privateStringvalidString; @Size(min=1,max=5) publicStringgetValidString(){ returnvalidString; } publicvoidsetValidString(finalStringvValidString){ validString=vValidString; } }
二、通过@Valid注解使用对象
@ResponseBody@RequestMapping(value="validObject",method=RequestMethod.POST) @ResponseStatus(HttpStatus.OK) publicStringvalidObject( @RequestBody() @ValidFoovFoo,BindingResultvBindingResult){ returnvFoo.getValidString(); }
校验RequestParams
使用校验bean的方式,没有办法校验RequestParam的内容,一般在处理Get请求的时候,会使用下面这样的代码:
@ResponseBody@RequestMapping(value="validString",method=RequestMethod.GET) @ResponseStatus(HttpStatus.OK) publicStringvalidString( @RequestParam(value="str",defaultValue="") StringvStr){ returnvStr; }
使用@Valid注解,对RequestParam对应的参数进行注解,是无效的,需要使用@Validated注解来使得验证生效。操作步骤如下:
一、声明错误处理类
packagecom.github.yongzhizhan.draftbox.controller; importorg.springframework.context.annotation.Bean; importorg.springframework.http.HttpStatus; importorg.springframework.stereotype.Component; importorg.springframework.validation.beanvalidation.MethodValidationPostProcessor; importorg.springframework.web.bind.annotation.ControllerAdvice; importorg.springframework.web.bind.annotation.ExceptionHandler; importorg.springframework.web.bind.annotation.ResponseBody; importorg.springframework.web.bind.annotation.ResponseStatus; importjavax.validation.ValidationException; @ControllerAdvice @Component publicclassGlobalExceptionHandler{ @Bean publicMethodValidationPostProcessormethodValidationPostProcessor(){ returnnewMethodValidationPostProcessor(); } @ExceptionHandler @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) publicStringhandle(ValidationExceptionexception){ System.out.println("badrequest,"+exception.getMessage()); return"badrequest,"+exception.getMessage(); } }
二、声明@Validated并加上校验注解
packagecom.github.yongzhizhan.draftbox.controller; importcom.github.yongzhizhan.draftbox.model.Foo; importorg.springframework.beans.factory.annotation.Value; importorg.springframework.http.HttpStatus; importorg.springframework.validation.BindingResult; importorg.springframework.validation.annotation.Validated; importorg.springframework.web.bind.annotation.RequestBody; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RequestMethod; importorg.springframework.web.bind.annotation.RequestParam; importorg.springframework.web.bind.annotation.ResponseBody; importorg.springframework.web.bind.annotation.ResponseStatus; importorg.springframework.web.bind.annotation.RestController; importjavax.validation.Valid; importjavax.validation.constraints.Size; @RestController @SuppressWarnings("UnusedDeclaration") @Validated publicclassIndexController{ @ResponseBody @RequestMapping(value="validString",method=RequestMethod.GET) @ResponseStatus(HttpStatus.OK) publicStringvalidString( @RequestParam(value="str",defaultValue="") @Size(min=1,max=3) StringvStr){ returnvStr; } }
代码:spring-mvc-validator_jb51.rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。