【发布时间】:2017-11-23 13:57:07
【问题描述】:
我的团队为我的项目实现了声纳代码覆盖,并且在课堂上抱怨错误“要么删除类“ResponseEntity”的这个无用的对象实例化,要么使用它”的方法之一。如果我删除它抱怨的行,它将起作用。但我也想处理这个错误。
任何如何处理的建议将不胜感激
@RequestMapping(value = "/**/identity", method = RequestMethod.POST)
public ResponseEntity<String> createIdentity(@RequestBody @NotNull Heartbeat heartbeat) {
//Validate
if (StringUtils.isEmpty(heartbeat.getHostname())
|| StringUtils.isEmpty(heartbeat.getEnvironment())
|| StringUtils.isEmpty(heartbeat.getProcessSignature())) {
return new ResponseEntity<String>(HttpStatus.BAD_REQUEST);
}
try {
byte[] encodedValue = identityService.createIdentity(heartbeat.getHostname(), heartbeat.getEnvironment(),
heartbeat.getProcessSignature());
return ResponseEntity.ok(new String(encodedValue));
} catch (BadPaddingException | IllegalBlockSizeException e) {
log.error("Unable to create entity for the request", e);
new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); // Sonar Complaint
}
return ResponseEntity.ok().build();
}
【问题讨论】:
-
不使用实例为什么要创建?你的意思是退货吗?
-
是的,安迪。它会返回错误详细信息。顺便说一句,这不是我的代码...
-
不,它不是返回错误详细信息,而是返回
return ResponseEntity.ok().build();。 -
@RamKarlapudi 添加到 Andy Turner 的评论中:“投诉行”中(很可能)缺少退货
-
是的,我删除了如下不需要的返回消息并进行了测试,该错误在声纳中消失了。返回 ResponseEntity.ok().build();