6

While using @RequestBody StreamSource found out, that if xml in request body in not valid StreamSource throws an Exception(resulting in 400 Bad Request) and i'm not able to handle it(tell client what is bad).

Is there a way to handle such exception?

skaffman
  • 398,947
  • 96
  • 818
  • 769

1 Answers1

10

In general You can catch exception in Spring MVC that way:

@ExceptionHandler(Exception.class)
public ModelAndView handleMyException(Exception  exception) {
    ModelAndView modelAndView = new ModelAndView("/errors/404");
    modelAndView.addObject("message", exception.getMessage());
    return modelAndView;
} 

You can map it to any exception time and redirect user to any page with any mesage.

Alternatively: you can return it in @ResponseBody:

  @ExceptionHandler(Exception.class)
  @ResponseBody
  public String handleMyException(Exception  exception) {
      return exception.getMessage();
  } 
Kyle
  • 3,549
  • 1
  • 19
  • 25
danny.lesnik
  • 18,479
  • 29
  • 135
  • 200