2017-02-09 30 views
1

我想在bindingresult haserror active時返回http錯誤或退出方法。我該怎麼辦?Spring MVC bindingresult haserror return

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

回答

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

非常感謝,它沒有創建新節點時,有錯誤;) –

+0

接受答案,如果它幫助,謝謝。 – mhshimul

相關問題