【问题标题】:Classes from "com.sun.*" and "sun.*" packages should not be used Sonar issue for Jersey client“com.sun.*”和“sun.*”包中的类不应用于 Jersey 客户端的声纳问题
【发布时间】:2015-12-19 07:07:17
【问题描述】:

我正在使用jersey client 进行休息通话。我的代码的导入是:

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

一切正常。我正在使用Sonar 来检查我的代码质量。

声纳显示出一个主要问题:

不应使用“com.sun.”和“sun.”包中的类

使用来自 sun 的类实际上是不好的做法吗?

如果是,有哪些替代方案?

【问题讨论】:

  • 关于com.sun 类的警告不正确。其中有很多,它们是 API 的文档化部分。例如,JNDI 充满了它们。必须避免使用 sun.* 类。

标签: java jersey sonarqube


【解决方案1】:

因为它们是内部 API:它们可能会以未记录或不受支持的方式进行更改,并且它们绑定到特定的 JRE/JDK(在您的情况下是 Sun),从而限制了程序的可移植性。

尽量避免使用此类 API,始终更喜欢公开记录和指定的类。

参考-It is a bad practice to use Sun's proprietary Java classes?

【讨论】:

  • com.sun. jersey 库的类不是内部类。
  • 请您详细说明您的评论?我读过 com.sun.* 是如上所述的内部结构
  • @FGI 这个问题主要是关于sun.*。大多数(如果不是全部)关于 com.sun.* 在该问答页面中是私有的论点都缺乏可靠的引用。
【解决方案2】:

最好迁移到 JAX-RS 2.0 客户端类。不过,一些重构是必要的。请参阅migration guide。例如,如果你以前是这样写的:

Client client = Client.create();
WebResource webResource = client.resource(restURL).path("myresource/{param}");
String result = webResource.pathParam("param", "value").get(String.class);

你现在应该这样写:

Client client = ClientFactory.newClient();
WebTarget target = client.target(restURL).path("myresource/{param}");
String result = target.pathParam("param", "value").get(String.class);

【讨论】:

    猜你喜欢
    • 2017-10-07
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    相关资源
    最近更新 更多