1

Hi I am wondering how to pass information to the exception handler. Say for example I doing validation. Whit @Valid. I can catch that specific exception but that does not tell me if was the first name or last name of the person that was wrong.

Maybe a custom exception with a error field attribute. If I do that is how to I through it? @Valid have already thrown an exception if validation fails.

Can i check the bindingresult for error and throw my custom exception? How do solve this?

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.