Checkstyle错误 - 返回计数为4(最大允许为2)

时间:2016-08-21 17:22:10

标签: java checkstyle

我不确定这是否是一个重复的问题。

我有一个带有4个return语句的Java方法,每个return语句返回一个不同的HTTP状态。

如何修复此checkstyle错误以减少返回语句。

if (condition) {
return new ResponseEntity<Domain>(domain, HttpStatus.CONFLICT);
            } else if (condition2) {
return new ResponseEntity<Domain>(domain, HttpStatus.PRECONDITION_FAILED);
            } else {
return new ResponseEntity<Domain>(domain, HttpStatus.OK);
}

1 个答案:

答案 0 :(得分:2)

您可以定义HttpStatus,然后只使用一个return语句:

HttpStatus status;
if (condition) {
    status = HttpStatus.CONFLICT;
else if (condition2) {
    status = HttpStatus.PRECONDITION_FAILED;
else {
    status = HttpStatus.OK;
}

return new ResponseEntity<Domain>(domain, status);
相关问题