spring mvc + @Valid + @RequestBody 接收json同时校验javaBean的数据有效性

本文介绍了一个使用Spring框架进行数据验证的实际案例,包括从版本3.1.0.CI-995升级到3.1.0.RELEASE的过程,以及如何在接收JSON数据时确保对象的有效性。文章详细说明了两种不同的验证方法及其适用场景。
1. 起因:
半路接手的项目,原使用spring 3.1.0.CI-995(不知道当时怎么设计的,使用了这么一个过渡版本),但是现在想用注解来验证数据的有效性,正常情况下,使用@Valid验证也没有什么问题,但是现在有一个需求就是接收json后,验证对象的数据有效性,这时后台出错。

2. 解决方法
spring 3.1.0.CI-995 升级到 3.1.0.RELEASE。版本升级过程中也不是很顺利,调了很长时间才通过,过渡版本到正式版也有很多差异的地方。

2. 普通验证
JavaBean

public class Message{

@NotEmpty(message = "Message name must not be blank!")
private String name;

@NotBlank(message = "Message description must not be blank!")
private String description;

public Message() {
}

public Message(String name, String description) {
this.name = name;
this.description = description;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

}


Controller

@RequestMapping("/sender/message")
@ResponseBody
public ResponseBean sendMessage(@Valid Message messageBean, BindingResult bindingResult){

......

if (bindingResult.hasErrors()) {
System.out.println("验证失败!");
}
......
}

[color=red]注意:BindingResult 对象必须在 @Valid 的紧挨着的后面,否则接收不到错误信息。[/color]

3. 接收json的验证
Controller
方法1:

@RequestMapping("/sender/message")
@ResponseBody
public ResponseBean sendMessage(@Valid @RequestBody Message messageBean, BindingResult bindingResult){
......

if (bindingResult.hasErrors()) {
System.out.println("验证失败!");
}
......
}

上面这种方式在spring3.2.x中没有试过,在3.1.0.RELEASE中是出错的。如果上面这种出错,可以使用下面方法。

方法2:

@RequestMapping("/sender/message")
@ResponseBody
public ResponseBean sendMessage(@Valid @RequestBody Message messageBean){
......

......
}

在此Controller中添加如下方法:

使用@ExceptionHandler捕获错误信息:

// valid exception
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public ResponseBean handleMethodArgumentNotValidException(
MethodArgumentNotValidException ex) {
BindingResult bindingResult = ex.getBindingResult();
String errorMesssage = "Invalid Request:";

for (FieldError fieldError : bindingResult.getFieldErrors()) {
errorMesssage += fieldError.getDefaultMessage() + ", ";
}

System.out.println(bindingResult.getFieldError().getDefaultMessage());
ResponseBean response = new ResponseBean();
response.setErrcode("-11");
response.setErrmsg(errorMesssage);
return response;
}

// JSON convert exception
@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseBody
public ResponseBean handleHttpMessageNotReadableException(
HttpMessageNotReadableException ex) {
ResponseBean response = new ResponseBean();
response.setErrcode("-22");
response.setErrmsg("json convert failure!");
return response;
}


[color=red]注意:MethodArgumentNotValidException 类,在spring 3.1.0.RELEASE 版本之后才有。[/color]

转载请注明:[url]http://langmnm.iteye.com/blog/2078439[/url]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值