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("验证失败!");
}
......
}
注意:BindingResult 对象必须在 @Valid 的紧挨着的后面,否则接收不到错误信息。
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;
}
注意:MethodArgumentNotValidException 类,在spring 3.1.0.RELEASE 版本之后才有。
转载请注明:
http://langmnm.iteye.com/blog/2078439
分享到:
相关推荐
使用环境:MyEclipse/Eclipse + Tomcat + MySQL。 使用技术:Spring MVC + Spring + MyBatis / JSP + Servlet + JavaBean + JDBC。
使用环境: MyEclipse/Eclipse + Tomcat + MySQL。...使用技术: Spring MVC + Spring + MyBatis 或 JSP + Servlet + JavaBean + JDBC。 效果:https://ymjin.blog.csdn.net/article/details/119986708
使用环境:MyEclipse/Eclipse + Tomcat + MySQL。...使用技术:Spring MVC + Spring + MyBatis 或 JSP + Servlet + JavaBean + JDBC。 演示地址:https://ymjin.blog.csdn.net/article/details/120991940
使用环境:MyEclipse/Eclipse + Tomcat + MySQL。 使用技术:Spring MVC + Spring + MyBatis 或 JSP + Servlet + JavaBean + JDBC。 https://ymjin.blog.csdn.net/article/details/120785168
使用环境:MyEclipse/Eclipse + Tomcat + MySQL。...使用技术:Spring MVC + Spring + MyBatis 或 JSP + Servlet + JavaBean + JDBC。 演示地址:https://ymjin.blog.csdn.net/article/details/121012207
使用环境:MyEclipse/Eclipse + Tomcat + MySQL。 使用技术:Spring MVC + Spring + MyBatis、JSP + Servlet + JavaBean + JDBC。
使用环境:MyEclipse/Eclipse + Tomcat + MySQL。 使用技术:Spring MVC + Spring + MyBatis / JSP + Servlet + JavaBean + JDBC。
使用环境:MyEclipse/Eclipse + Tomcat + MySQL。 使用技术:Spring MVC + Spring + MyBatis / JSP + Servlet + JavaBean + JDBC。
使用环境:MyEclipse/Eclipse + Tomcat + MySQL。 使用技术:Spring MVC + Spring + MyBatis / JSP + Servlet + JavaBean + JDBC。
使用环境:MyEclipse/Eclipse + Tomcat + MySQL。...使用技术:Spring MVC + Spring + MyBatis 或 JSP + Servlet + JavaBean + JDBC。 效果:https://ymjin.blog.csdn.net/article/details/120000792
使用环境:MyEclipse/Eclipse + Tomcat + MySQL。 使用技术:Spring MVC + Spring + MyBatis / JSP + Servlet + JavaBean + JDBC。
使用环境:MyEclipse/Eclipse + Tomcat + MySQL。 使用技术:Spring MVC + Spring + MyBatis / JSP + Servlet + JavaBean + JDBC。
使用环境:MyEclipse/Eclipse + Tomcat + MySQL。 使用技术:Spring MVC + Spring + MyBatis / JSP + Servlet + JavaBean + JDBC。
使用环境:MyEclipse/Eclipse + Tomcat + MySQL。 使用技术:Spring MVC + Spring + MyBatis / JSP + Servlet + JavaBean + JDBC。
使用环境:MyEclipse/Eclipse + Tomcat + MySQL。 使用技术:Spring MVC + Spring + MyBatis / JSP + Servlet + JavaBean + JDBC。
使用环境:MyEclipse/Eclipse + Tomcat + MySQL。 使用技术:Spring MVC + Spring + MyBatis 或 JSP + Servlet + JavaBean + JDBC。 https://ymjin.blog.csdn.net/article/details/120365647
很好的资源哦
jsp+servlet+javaBean实现MVC源码 jsp+servlet+javaBean实现MVC源码 jsp+servlet+javaBean实现MVC源码 jsp+servlet+javaBean实现MVC源码 jsp+servlet+javaBean实现MVC源码 jsp+servlet+javaBean实现MVC源码 jsp+...
jsp+servlet+javaBean实现MVC_jspmvcjsp+servlet+javaBean实现MVC_jspmvcjsp+servlet+javaBean实现MVC_jspmvcjsp+servlet+javaBean实现MVC_jspmvcjsp+servlet+javaBean实现MVC_jspmvcjsp+servlet+javaBean实现MVC_...
主要功能:商品管理,员工管理,供应商管理和管理员管理等 采用技术:DAO、POJO、MVC模式DAO. Servlet, JSP 本系统实现了商品管理、员工管理、供应商管理和管理员管理,管理员可以对上架商品、库存商品、新增...