Spring3 nie działa @ poprawny, gdy żądanie jsona mam błąd 400 Błędne żądanie

Korzystam z frameworka Spring3, walidatora hibernacji i jacksona.

kiedy żądam jsondata na serwer. zwrócił błąd 400 Bad Request.

wtedy moje dane są typu missmatch.

kiedy żądam typu dopasowania, to działa dobrze.

Mój kod kontrolera:

@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;
}

iMetoda obsługi błędów jest:

@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");
}

Customer.java (model):

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;
    }
}

dane json:

1.{"name":"ken","age":11,"updateTime":"2013-06-18"}
2.{"name":"ken","age":"","updateTime":""}
3.{"name":"ken","age":"joe","updateTime":""}

Dane json 1 są zwracane normalnie.

Dane json 2 są zwracane normalnie (złapałem MethodArgumentNotValidException customer.age, NotNull).

Zwracane są dane json 2 400 Błąd błędu żądania. ale mam nadzieję, że serwer zwrócił błąd typeMismatch.int.

@ Pavel Horal Dziękuję bardzo!

Mogłem złapać wyjątek HttpMessageNotReadableException i obsłużyć.

Metody obsługi błędów uległy zmianie.

    @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());
        }
    }
・・・

Jednak to, że dziedziczy się po to, aby utworzyć klasę bazową lub napisać ten kod we wszystkich kontrolerach, wydaje mi się, że nie widziałem koncepcji projektu wiosny, ponieważ zdecydowano się tam przetworzyć, aby nowy wyjątek był wyjątkiem.

Mój nowy ExceptionResolver to:

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{
                 ・・・

I dodałem kadzidło do 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>

Ten sposób myślenia jest prawidłowy?

Dziękuję Ci,

questionAnswers(1)

yourAnswerToTheQuestion