类 from "com.sun.*" and "sun.*" packages should not be used Sonar issue for Jersey client
Classes from "com.sun.*" and "sun.*" packages should not be used Sonar issue for Jersey client
我正在使用 jersey client
进行休息。我的代码的导入是:
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
一切正常。我正在使用 Sonar
检查我的代码质量。
sonar 正在显示一个主要问题:
Classes from "com.sun." and "sun." packages should not be used
使用来自 sun 的 类 实际上是不好的做法吗?
如果是,有哪些选择?
因为它们是内部 API:它们可能会以未记录或不受支持的方式进行更改,并且它们绑定到特定的 JRE/JDK(在您的情况下是 Sun),限制了程序的可移植性。
尽量避免使用此类 API,始终首选 public 记录和指定 class。
参考- It is a bad practice to use Sun's proprietary Java classes?
最好迁移到 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);
我正在使用 jersey client
进行休息。我的代码的导入是:
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
一切正常。我正在使用 Sonar
检查我的代码质量。
sonar 正在显示一个主要问题:
Classes from "com.sun." and "sun." packages should not be used
使用来自 sun 的 类 实际上是不好的做法吗?
如果是,有哪些选择?
因为它们是内部 API:它们可能会以未记录或不受支持的方式进行更改,并且它们绑定到特定的 JRE/JDK(在您的情况下是 Sun),限制了程序的可移植性。
尽量避免使用此类 API,始终首选 public 记录和指定 class。
参考- It is a bad practice to use Sun's proprietary Java classes?
最好迁移到 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);