1

您好我想知道如何将信息传递给异常处理程序。比如说我在做验证。惠特@Valid。我可以捕捉到那个特定的异常,但这并不能告诉我是错误的人的名字还是姓氏。

可能是带有错误字段属性的自定义异常。如果我这样做,我将如何通过它?如果验证失败,@Valid 已经抛出异常。

我可以检查绑定结果是否有错误并抛出我的自定义异常吗?如何解决这个问题?

@RequestMapping(value = "/post", method = RequestMethod.POST)
public String post(@Valid Person person) {
    System.out.println(person);
    System.out.println(person2);

    return "home";
}

@ExceptionHandler(Exception.class)
@ResponseBody
public List<FailureResult> handleException
    (Exception re, HttpServletResponse response) {

    FailureResult failureResult = new FailureResult();
    failureResult.setName("name");
            //wich feild failed the validation?


    List<FailureResult> r = new ArrayList<FailureResult>();
    r.add(failureResult);

    return r;
}
4

0 回答 0