SpringMVC对post提交的对象和对get提交的单个参数分别是如何校验的

概述

Spring MVC支持Bean Validation,通过这个验证技术,可以通过注解方式,很方便的对输入参数进行验证,之前使用的校验方式,都是基于Bean对象的,但是在@RequestParam中,没有Bean对象,这样使得校验无法进行,可以通过使用@Validated注解,使得校验可以进行。

校验bean对象

一般校验bean对象,为了可以自动的校验属性,可以通过两步解决:

一、声明对象

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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注解使用对象

?
1
2
3
4
5
6
7
8
@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请求的时候,会使用下面这样的代码:

?
1
2
3
4
5
6
7
8
@ResponseBody @RequestMapping (value = "validString" , method = RequestMethod.GET)
@ResponseStatus (HttpStatus.OK)
public String validString(
     @RequestParam (value = "str" , defaultValue = "" )
     String vStr){
 
   return vStr;
}

使用@Valid注解,对RequestParam对应的参数进行注解,是无效的,需要使用@Validated注解来使得验证生效。操作步骤如下:

一、声明错误处理类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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并加上校验注解

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
   }
}

代码:spring-mvc-validator_jb51.rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

原文链接:http://www.cnblogs.com/jeffen/p/6402475.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值