【问题标题】:Sonar Complaint Java + Either remove this useless object instantiation of class "ResponseEntity" or use it声纳投诉Java +删除类“ResponseEntity”的这个无用对象实例化或使用它
【发布时间】: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();

标签: java sonarqube


【解决方案1】:

问题是您没有返回您认为您处于错误场景中的响应。您可以通过正确返回 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();
}

【讨论】:

    猜你喜欢
    • 2021-02-11
    • 2021-11-23
    • 2018-10-18
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多