Search code examples
javaspringvalidationspring-mvcspring-validator

Spring MVC bindingresult haserror return


I want to return http error or exit from method when bindingresult haserror active .How i can do it ?

 @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Node create(@Valid @RequestBody Node node, BindingResult bindingResult) {
        LOG.info(String.format("Create new Node: %s", node));
        if (!bindingResult.hasErrors()) {
                return nodeService.create(node);
        }
        else{
            // How i can exit without return any Node object ?
        }
    }

Solution

  • Just return null.

        @RequestMapping(value = "/create", method = RequestMethod.POST)
        public Node create(@Valid @RequestBody Node node, BindingResult bindingResult) {
            LOG.info(String.format("Create new Node: %s", node));
            if (!bindingResult.hasErrors()) {
                return nodeService.create(node);
            } else {
                return null;
            }
        }