gpt4 book ai didi

java - 错误: Missing return statement when throwing a new ResponseStatusException in method

转载 作者:行者123 更新时间:2023-12-01 17:58:54 33 4
gpt4 key购买 nike

当尝试在方法中抛出新异常而不是直接抛出异常时,我的 IDE 会在 catch block 中显示错误“缺少 return 语句”。

REST Controller 的缩小示例:

@RestController
public class RootController {
protected final Logger log = Logger.getLogger("myLogger");

@PostMapping(
value={"sample/",
"sample/123"},
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Object getFileContentPost (@RequestBody Request payload) {
return checkPayloadAndRespond(payload.getFileName());
}

private Object checkPayloadAndRespond(String fileName) throws ResponseStatusException {
if (StringUtils.isEmpty(fileName))
logErrorAndThrowResponseException(HttpStatus.BAD_REQUEST, "Field fileName is empty or missing!", null);

try {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue("/static/json/" + fileName , Object.class);
} catch (IOException e) {
//line causing ERROR "missing return statement":
logErrorAndThrowResponseException(HttpStatus.INTERNAL_SERVER_ERROR, "Could not parse file!, e);
}
}

private void logErrorAndThrowResponseException(HttpStatus status, String reason, @Nullable Throwable cause) throws ResponseStatusException {
log.error("Error occurred: " + reason + ", returning status: " + status);
throw new ResponseStatusException(status, reason, cause);
}


}

工作示例:如果我直接使用 throw new 语句而不使用方法:

@RestController
public class RootController {
protected final Logger log = Logger.getLogger("myLogger");

@PostMapping(
value={"sample/",
"sample/123"},
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Object getFileContentPost (@RequestBody Request payload) {
return checkPayloadAndRespond(payload.getFileName());
}

private Object checkPayloadAndRespond(String fileName) throws ResponseStatusException {
if (StringUtils.isEmpty(fileName))
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Field fileName is empty or missing!", e);

try {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue("/static/json/" + fileName , Object.class);
} catch (IOException e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Could not parse response file!", e);
}
}

}

解决这个问题的最佳方法是什么?

我想做的就是在抛出 ResponseStatusException 时实际创建一个日志。

提前致谢。

最佳答案

所以我猜想 void-return-type 导致了这个问题。

我现在通过返回实际响应来解决它,然后抛出:

  private Object checkPayloadAndRespond(String fileName) throws ResponseStatusException {
if (StringUtils.isEmpty(fileName))
throw logErrorAndThrowResponseException(HttpStatus.BAD_REQUEST, "Field fileName is empty or missing!", null);

try {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue("/static/json/" + fileName , Object.class);
} catch (IOException e) {
throw logErrorAndThrowResponseException(HttpStatus.INTERNAL_SERVER_ERROR, "Could not parse file!, e);
}
}

private ResponseStatusException logErrorAndThrowResponseException(HttpStatus status, String reason, @Nullable Throwable cause) {
log.error("Error occurred: " + reason + ", returning status: " + status);
return new ResponseStatusException(status, reason, cause);
}


}

关于java - 错误: Missing return statement when throwing a new ResponseStatusException in method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60668165/

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