7

I am using jersey client for rest calls. Imports for my code are:

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

Everything is working fine. I am using Sonar for checking my Code quality.

sonar is showing a major issue for:

Classes from "com.sun." and "sun." packages should not be used

Is this actually bad practice to use classes from sun?

If yes, what are the alternatives?

Dev
  • 13,492
  • 19
  • 81
  • 174
  • 3
    The warning about `com.sun` classes is incorrect. There are plenty of these and they are documented parts of the API. JNDI for example is full of them. It is the `sun.*` classes that must be avoided. – user207421 Sep 22 '15 at 10:37

2 Answers2

5

It's better to migrate to JAX-RS 2.0 client classes. Some refactoring would be necessary though. See the migration guide. For example, if you wrote like this before:

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

You should write now like this:

Client client = ClientFactory.newClient();
WebTarget target = client.target(restURL).path("myresource/{param}");
String result = target.pathParam("param", "value").get(String.class);
Jasha
  • 781
  • 9
  • 15
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
0

Because they are internal APIs: they are subject to change in a undocumented or unsupported way and they are bound to a specific JRE/JDK (Sun in your case), limiting portability of your programs.

Try to avoid uses of such APIs, always prefer a public documented and specified class.

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

Community
  • 1
  • 1
Techidiot
  • 1,921
  • 1
  • 15
  • 28
  • 3
    The com.sun. classes of the jersey library are not internal classes. – nos Sep 22 '15 at 10:51
  • Please could you detail your comment? I read com.sun.* are internals as described above – FGI Jan 20 '16 at 14:41
  • @FGI That question is mainly about `sun.*`. Most (if not all) arguments about `com.sun.*` being private in that Q&A page lack reliable citation. – Franklin Yu Apr 09 '19 at 02:41