gpt4 book ai didi

java - Spring MVC 绑定(bind)结果有错误返回

转载 作者:太空宇宙 更新时间:2023-11-04 11:48:05 25 4
gpt4 key购买 nike

当绑定(bind)结果有错误处于 Activity 状态时,我想返回 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 ?
}
}

最佳答案

直接返回 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;
}
}

关于java - Spring MVC 绑定(bind)结果有错误返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42129223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com