1.实体类属性添加注解
import java.util.Date;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.DateTimeFormat;
public class Student {
@Length(min=4,max=10,message="字段长度不能超过4至10个字符")
private String name ;
@NotEmpty(message="年龄不能为空!")
private int age ;
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date date ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", date=" + date
+ "]";
}
}
2.控制层使用实体对象使用注解
import javax.validation.Valid;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.liuchao.domain.Student;
@Controller
public class IndexController {
private static final Logger log = Logger.getLogger(IndexController.class);
@RequestMapping("/test")
public String test(@Valid Student student){
return "index";
}
}
3.声明统一异常处理类,捕捉不同类型的异常
import java.io.IOException;
import java.lang.reflect.UndeclaredThrowableException;
import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import com.liuchao.bean.APPFinalConfig;
import com.liuchao.bean.JResult;
@ControllerAdvice
@ResponseBody
public class ExceptionHandle {
public static final Logger log = Logger.getLogger(ExceptionHandle.class);
/**
* @Description: 对象参数异常处理
* @author lc
* @param ex
* @throws
* @return JResult 返回类型
*/
@ExceptionHandler(BindException.class)
@ResponseBody
public JResult bindException(BindException ex) {
log.error(APPFinalConfig.ERROR, ex);
JResult result = new JResult();
result.setErrorCode(APPFinalConfig.ERROR);
result.setState("-1001");
StringBuilder msg = new StringBuilder() ;
BindException exception = (BindException)ex ;
for (FieldError error : exception.getBindingResult().getFieldErrors()) {
/*msg.append("参数");
msg.append(error.getField());
msg.append("=");
msg.append(error.getRejectedValue());
msg.append(",说明:");*/
msg.append(error.getDefaultMessage());
}
result.setDescription(msg.toString());
return result ;
}
/**
* @Title: exceptionHandle
* @Description: 参数异常处理
* @author lc
* @param ex
* @throws IOException
* @return JResult 返回类型
*/
@ExceptionHandler(UndeclaredThrowableException.class)
@ResponseBody
public JResult parameterException(UndeclaredThrowableException ex){
log.error(APPFinalConfig.ERROR, ex);
JResult result = new JResult();
result.setErrorCode(APPFinalConfig.ERROR);
result.setState("-1001");
result.setDescription(ex.getCause().getMessage());
return result ;
}
/**
* @Title: exceptionHandle
* @Description: 默认异常处理
* @author lc
* @param ex
* @throws IOException
* @return JResult 返回类型
*/
@ExceptionHandler(Exception.class)
@ResponseBody
public JResult exceptionHandle( Exception ex) {
log.error(APPFinalConfig.ERROR, ex);
ex.printStackTrace();
JResult result = new JResult();
result.setErrorCode(APPFinalConfig.ERROR);
result.setState("-1001");
result.setDescription("系统出现异常");
return result ;
}
}