Spring MVC bindingresult haserror返回

时间:2017-02-09 05:53:15

标签: java spring validation spring-mvc spring-validator

我想在绑定结果haserror激活时返回http错误或退出方法。我能做到吗?

 @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 ?
        }
    }

1 个答案:

答案 0 :(得分:0)

只需返回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;
        }
    }