简体   繁体   English

声纳违规使用spring ExceptionHandler时从Exception进行未经检查/未经确认的转换

[英]Sonar violation Unchecked/unconfirmed cast from Exception while using spring ExceptionHandler

I'm using SpringFramework 4.3.2 and SonarQube v5.6.1 with FindBugs and getting the following violation: 我正在使用带有FindBugs的SpringFramework 4.3.2和SonarQube v5.6.1并遇到以下冲突:

Unchecked/unconfirmed cast from Exception to java.rmi.RemoteException 未经检查/未经确认的从Exception强制转换为java.rmi.RemoteException

this is the original code: 这是原始代码:

    @ExceptionHandler(RemoteException.class)
     public ResponseEntity<ClientErrorInformation> resourceRemoteExceptionHandler(HttpServletRequest req, Exception e){
      RemoteException re = (RemoteException) e;

      ...

      return new ResponseEntity<ClientErrorInformation>(new ClientErrorInformation(null, "Internal server error", req.getRequestURI(), false,null), HttpStatus.INTERNAL_SERVER_ERROR);          
    }
}

When using the spring @ExceptionHandler annotation ,it means that only the specified kind of exception will be set as argument to the method. 使用spring @ExceptionHandler批注时,这意味着仅将指定种类的异常设置为方法的参数。 in this case RemoteException. 在这种情况下,为RemoteException。

Can you please tell me what's wrong with that code? 您能告诉我该代码有什么问题吗? or i can ignore that kind of violations in this code. 或者我可以忽略此代码中的此类违规行为。

Thanks 谢谢

When using the spring @ExceptionHandler annotation ,it means that only the specified kind of exception will be set as argument to the method. 使用spring @ExceptionHandler批注时,这意味着仅将指定种类的异常设置为方法的参数。 in this case RemoteException. 在这种情况下,为RemoteException。

Can you please tell me what's wrong with that code? 您能告诉我该代码有什么问题吗? or i can ignore that kind of violations in this code. 或者我可以忽略此代码中的此类违规行为。

Spring knows what the caught exception is of RemoteException type but FindBugs doesn't rely on Spring implementation to detect potential rule violation. Spring知道所捕获的异常属于RemoteException类型,但FindBugs并不依赖Spring实现来检测潜在的规则违规。 From FindBugs view, it sees only a cast without check. 从FindBugs视图中,它只能看到未经检查的演员表。 That's why it indicates a potential rule violation. 这就是为什么它表示潜在的违反规则的原因。 In your case, you don't need to ignore the violation since Spring allows to specify the exception type as you wish. 在您的情况下,您不需要忽略违规,因为Spring允许根据需要指定异常类型。

From http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/ExceptionHandler.html 来自http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/ExceptionHandler.html

org.springframework.web.bind.annotation org.springframework.web.bind.annotation

Annotation Type ExceptionHandler 注释类型ExceptionHandler

Handler methods which are annotated with this annotation are allowed to have very flexible signatures. 使用此注释注释的处理程序方法具有非常灵活的签名。 They may have parameters of the following types, in arbitrary order: 它们可以具有任意类型的以下类型的参数:

An exception argument: declared as a general Exception or as a more specific exception... 异常参数:声明为一般异常或更具体的异常...

So, as @chrylis suggested, you should declare your method like that to avoid the cast : 因此,正如@chrylis建议的那样,您应该像这样声明您的方法以避免强制转换:

public ResponseEntity<ClientErrorInformation> resourceRemoteExceptionHandler(HttpServletRequest req, RemoteException e)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM