关于 sonarQube检测到 Connection 的 try-with-resources 问题

我在写代码的过程中调取接口使用以下代码

调用没问题,使用起来也没问题,但是上传到 sonarQube 上一检测就出现了 try-with-resources 的规范检测,领导说要修改,所以我就去修改了,  这个规范意思是要用 jdk1.7以后的自动关闭资源策略   大致是  实现 AutoCloseable 接口  然后将try-catch-finally改成  try(  资源  ){  ... }catch{ ... }

修改时发现 try-with-resources 并不支持 connection的自动关闭,于是我查看了一下apche官方的httpclient文档

连接:http://hc.apache.org/httpcomponents-client-4.5.x/quickstart.html

将代码修改为如下所示:

修改后再上传  OK ! sonarQube 检测通过!

希望能帮到各位。

猜你喜欢

转载自blog.csdn.net/qq_36380675/article/details/82982508