详解在Spring MVC中使用注解的方式校验RequestParams
程序员文章站
2024-03-04 14:14:05
概述
spring mvc支持bean validation,通过这个验证技术,可以通过注解方式,很方便的对输入参数进行验证,之前使用的校验方式,都是基于bean对象的,...
概述
spring mvc支持bean validation,通过这个验证技术,可以通过注解方式,很方便的对输入参数进行验证,之前使用的校验方式,都是基于bean对象的,但是在@requestparam中,没有bean对象,这样使得校验无法进行,可以通过使用@validated注解,使得校验可以进行。
校验bean对象
一般校验bean对象,为了可以自动的校验属性,可以通过两步解决:
一、声明对象
package com.github.yongzhizhan.draftbox.model; import javax.validation.constraints.size; /** * 带验证的对象 * @author zhanyongzhi */public class foo { private string validstring; @size(min = 1, max = 5) public string getvalidstring() { return validstring; } public void setvalidstring(final string vvalidstring) { validstring = vvalidstring; } }
二、通过@valid注解使用对象
@responsebody@requestmapping(value = "validobject", method = requestmethod.post) @responsestatus(httpstatus.ok) public string validobject( @requestbody() @valid foo vfoo, bindingresult vbindingresult){ return vfoo.getvalidstring(); }
校验requestparams
使用校验bean的方式,没有办法校验requestparam的内容,一般在处理get请求的时候,会使用下面这样的代码:
@responsebody@requestmapping(value = "validstring", method = requestmethod.get) @responsestatus(httpstatus.ok) public string validstring( @requestparam(value = "str", defaultvalue = "") string vstr){ return vstr; }
使用@valid注解,对requestparam对应的参数进行注解,是无效的,需要使用@validated注解来使得验证生效。操作步骤如下:
一、声明错误处理类
package com.github.yongzhizhan.draftbox.controller; import org.springframework.context.annotation.bean; import org.springframework.http.httpstatus; import org.springframework.stereotype.component; import org.springframework.validation.beanvalidation.methodvalidationpostprocessor; import org.springframework.web.bind.annotation.controlleradvice; import org.springframework.web.bind.annotation.exceptionhandler; import org.springframework.web.bind.annotation.responsebody; import org.springframework.web.bind.annotation.responsestatus; import javax.validation.validationexception; @controlleradvice @component public class globalexceptionhandler { @bean public methodvalidationpostprocessor methodvalidationpostprocessor() { return new methodvalidationpostprocessor(); } @exceptionhandler @responsebody @responsestatus(httpstatus.bad_request) public string handle(validationexception exception) { system.out.println("bad request, " + exception.getmessage()); return "bad request, " + exception.getmessage(); } }
二、声明@validated并加上校验注解
package com.github.yongzhizhan.draftbox.controller; import com.github.yongzhizhan.draftbox.model.foo; import org.springframework.beans.factory.annotation.value; import org.springframework.http.httpstatus; import org.springframework.validation.bindingresult; import org.springframework.validation.annotation.validated; import org.springframework.web.bind.annotation.requestbody; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.bind.annotation.responsebody; import org.springframework.web.bind.annotation.responsestatus; import org.springframework.web.bind.annotation.restcontroller; import javax.validation.valid; import javax.validation.constraints.size; @restcontroller @suppresswarnings("unuseddeclaration") @validated public class indexcontroller { @responsebody @requestmapping(value = "validstring", method = requestmethod.get) @responsestatus(httpstatus.ok) public string validstring( @requestparam(value = "str", defaultvalue = "") @size(min = 1, max = 3) string vstr){ return vstr; } }
代码:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。