我正在使用 Spring3 框架、休眠验证器和杰克逊。
当我向服务器请求 jsondata 时。返回 400 错误请求错误。
那么我的数据是不匹配类型。
当我请求匹配类型时,它运行良好。
我的控制器代码:
@RequestMapping(consumes = MediaType.ALL_VALUE, produces = MediaType.ALL_VALUE,value =
"doAdd", method = RequestMethod.POST)
@ResponseBody
public Customer doAdd(@RequestBody @Valid Customer inData){
this.customerService.addData(inData);
return inData;
}
和错误处理方法是:
@ExceptionHandler
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ResponseBody
public void ajaxValidationErrorHandle(MethodArgumentNotValidException errors ,
HttpServletResponse response) throws BusinessException {
List<String> resErrors = new ArrayList<String>();
for(ObjectError error : errors.getBindingResult().getAllErrors()){
resErrors.add(error.getCode());
}
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
throw new BusinessException("E000001", "VALIDATION");
}
客户.java(模型):
package test.business.model;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Range;
public class Customer {
@Size(min = 0, max = 3)
public String id;
@NotNull
@Size(min = 1)
public String name;
@NotNull
@Range(min = 10, max = 99)
public Integer age;
public String updateTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getUpdateTime() {
return updateTime;
}
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
}
json数据:
1.{"name":"ken","age":11,"updateTime":"2013-06-18"}
2.{"name":"ken","age":"","updateTime":""}
3.{"name":"ken","age":"joe","updateTime":""}
json数据1正常返回。
json 数据 2 正常返回(我发现 MethodArgumentNotValidException customer.age,NotNull)。
json 数据 2 返回 400 Bad Request 错误。但我希望服务器返回 typeMismatch.int 错误。
@Pavel Horal 非常感谢!
我可以捕获 HttpMessageNotReadableException 并进行处理。
已更改的错误处理方法。
@ExceptionHandler
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ResponseBody
public void ajaxValidationErrorHandle(Exception errors , HttpServletResponse response) throws BusinessException {
if(errors instanceof MethodArgumentNotValidException){
MethodArgumentNotValidException mane = (MethodArgumentNotValidException)errors;
for(ObjectError error : mane.getBindingResult().getAllErrors()){
resErrors.add(error.getCode());
}
}
if(errors instanceof HttpMessageNotReadableException){
JsonMappingException jme = (JsonMappingException)errors.getCause();
List<Reference> errorObj = jme.getPath();
for(Reference r : errorObj){
System.out.println(r.getFieldName());
}
}
・・・
但是,就是继承做基类或者在所有控制器中写这段代码,感觉好像没见过spring的设计理念,因为,决定去那边处理,新建一个ExceptionResolver。
我的新异常解析器是:
public class OriginalExceptionHandler extends SimpleMappingExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {
ModelAndView m = null;
StringBuilder exceptionMessage = new StringBuilder(ajaxDefaultErrorMessage) ;
if(e instanceof BusinessException){
・・・
}else if(e instanceof HttpMessageNotReadableException){
Throwable t = e.getCause();
if(t instanceof JsonMappingException){
JsonMappingException jme = (JsonMappingException)t;
List<Reference> errorObj = jme.getPath();
for(Reference r : errorObj){
exceptionMessage.append("VE9999:Unmatched Type Error!!("+r.getFieldName()+")");
}
}else{
exceptionMessage.append(e+"\n"+o.toString());
}
}else if(e instanceof MethodArgumentNotValidException){
MethodArgumentNotValidException mane = (MethodArgumentNotValidException)e;
for(ObjectError error : mane.getBindingResult().getAllErrors()){
if(error instanceof FieldError){
FieldError fe = (FieldError) error;
exceptionMessage.append("VE0001:Validation Error!!"+fe.getField()+"-"+fe.getDefaultMessage());
}else{
exceptionMessage.append("VE0001:Validation Error!!"+error.getDefaultMessage());
}
}
}else{
・・・
而且,我在 application-context.xml 中添加了香:
<bean class="test.core.OriginalExceptionHandler" p:order="1">
<property name="exceptionMappings">
<props>
<prop key="sample.core.BusinessException">
ExceptionPage
</prop>
<prop key="test.core.LoginSessionException">
LoginSessionException
</prop>
</props>
</property>
<property name="defaultErrorView" value="error" />
</bean>
这种思维方式正确吗?
谢谢,