使用 spring ExceptionHandler 时从 Exception 投射声纳违规 Unchecked/unconfirmed
Sonar violation Unchecked/unconfirmed cast from Exception while using spring ExceptionHandler
我将 SpringFramework 4.3.2 和 SonarQube v5.6.1 与 FindBugs 一起使用并遇到以下违规情况:
Unchecked/unconfirmed cast from Exception to java.rmi.RemoteException
这是原代码:
@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);
}
}
当使用spring @ExceptionHandler 注释时,这意味着只有指定类型的异常才会被设置为方法的参数。
在这种情况下 RemoteException.
你能告诉我这段代码有什么问题吗?或者我可以忽略此代码中的此类违规行为。
谢谢
When using the spring @ExceptionHandler annotation ,it means that only
the specified kind of exception will be set as argument to the method.
in this case RemoteException.
Can you please tell me what's wrong with that code? or i can ignore
that kind of violations in this code.
Spring 知道捕获的异常是 RemoteException
类型,但 FindBugs 不依赖 Spring 实现来检测潜在的规则违规。从 FindBugs 视图来看,它只看到一个未经检查的转换。这就是为什么它表示潜在的违规行为。
在您的情况下,您不需要忽略违规,因为 Spring 允许您根据需要指定异常类型。
org.springframework.web.bind.annotation
注解类型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...
因此,正如@chrylis 建议的那样,您应该这样声明您的方法以避免转换:
public ResponseEntity<ClientErrorInformation> resourceRemoteExceptionHandler(HttpServletRequest req, RemoteException e)
我将 SpringFramework 4.3.2 和 SonarQube v5.6.1 与 FindBugs 一起使用并遇到以下违规情况:
Unchecked/unconfirmed cast from Exception to java.rmi.RemoteException
这是原代码:
@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);
}
}
当使用spring @ExceptionHandler 注释时,这意味着只有指定类型的异常才会被设置为方法的参数。 在这种情况下 RemoteException.
你能告诉我这段代码有什么问题吗?或者我可以忽略此代码中的此类违规行为。
谢谢
When using the spring @ExceptionHandler annotation ,it means that only the specified kind of exception will be set as argument to the method. in this case RemoteException.
Can you please tell me what's wrong with that code? or i can ignore that kind of violations in this code.
Spring 知道捕获的异常是 RemoteException
类型,但 FindBugs 不依赖 Spring 实现来检测潜在的规则违规。从 FindBugs 视图来看,它只看到一个未经检查的转换。这就是为什么它表示潜在的违规行为。
在您的情况下,您不需要忽略违规,因为 Spring 允许您根据需要指定异常类型。
org.springframework.web.bind.annotation
注解类型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...
因此,正如@chrylis 建议的那样,您应该这样声明您的方法以避免转换:
public ResponseEntity<ClientErrorInformation> resourceRemoteExceptionHandler(HttpServletRequest req, RemoteException e)