0

I have a Spring MVC application where I use data binding to populate a custom form object someForm with the posted values. The interesting part of the controller looks like the following:

@RequestMapping(value = "/some/path", method = RequestMethod.POST)
public String createNewUser(@ModelAttribute("someForm") SomeForm someForm, BindingResult result){
    SomeFormValidator validator = new SomeFormValidator();
    validator.validate(someForm, result);

    if(result.hasErrors()){
        ...

        return "/some/path";
    }
}

SomeFormValidator class is implementing Springs org.springframework.validation.Validator interface. While this is great for validating the users input and creating error messages related to the input, this seems to be not well suited to handle more critical errors, which cannot be presented to the user but are still related to a controller input, like a missing hidden field which is expected to be present at post time. Such errors should result in application errors. What is the Spring MVC way to handle such errors?

1 Answer 1

1

What I usually do, I dont catch exceptions in the DAO and Service layes. I just throw them then I define ExceptionHandlers in the Controller class and in these ExceptionHandlers, I put my codes for handling such errors then redirect my users to a page saying something like

Fatal Error Occured. Please contact the administrators.

Here is the sample code of ExceptionHandler annotation

@Controller
public class MyController {

    @Autowired
    protected MyService myService;

    //This method will be executed when an exception of type SomeException1 is thrown
    //by one of the controller methods
    @ExceptionHandler(SomeException1.class)
    public String handleSomeException1(...) {
        //...
        //do some stuff
        //...

        return "view-saying-some-exception-1-occured";
    }

    //This method will be executed when an exception of type SomeException2 is thrown
    //by one of the controller methods
    @ExceptionHandler(SomeException2.class)
    public String handleSomeException2(...) {
        //...
        //do some stuff
        //...

        return "view-saying-some-exception-2-occured";
    }


    //The controller method that will entertain request mappings must declare 
    //that they can throw the exception class that your exception handler catches
    @RequestMapping(value = "/someUrl.htm", method = RequestMethod.POST)
    public String someMethod(...) throws SomeException1, SomeException2{

        //...
        //do some stuff, call to myService maybe
        //...

        return "the-happy-path-view-name";
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! So after using a Validator, I would have to check again for some critical validation errors and manually throw Exceptions (which are then handled by ExceptionHandlers), because Springs Errors class is only intended to keep error messages for the user, which are semantically different from application errors?
Yes, application errors are different and thus must be handled differently. Spring error classes are used to address binding errors between the form and its backing object. Just make sure you don't have a try...catch block in any of your Service-DAO-Controller codes.

Your Answer

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