gpt4 book ai didi

java - Sonar Complaint Java + 要么删除这个类 "ResponseEntity"的无用对象实例化,要么使用它

转载 作者:行者123 更新时间:2023-12-02 02:45:17 25 4
gpt4 key购买 nike

我的团队为我的项目实现了 Sonar 代码覆盖率,并且类中的一个方法提示错误“要么删除类“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();
}

最佳答案

问题是您没有返回您认为处于错误场景中的响应。您可以通过正确返回 INTERNAL_SERVER_ERROR 响应来解决此问题。

Sonar 只是指出了可能的缺陷。

@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);
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
return ResponseEntity.ok().build();
}

关于java - Sonar Complaint Java + 要么删除这个类 "ResponseEntity"的无用对象实例化,要么使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44662945/

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