Java client that consumes a RESTful web service created using Jersey 2

I'm learning web services using Java. I just don't know which method has what advantage over another, like which one is more secure? Which one will be more faster?

POST resource is as follows:

@POST
@Path("postactivity")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String createActivity(
@QueryParam("id") int id,
@QueryParam("description") String description,
@QueryParam("duration") int duration,
@QueryParam("name")String name)
{
//My code that creates Activity object from QueryParams is here.
}


Now I've created a client which is a Java application. I'm consuming the REST service in the following two ways:

Method 1 - using HTTPURLConnection

private static void doPost(){
QueryString qs = new QueryString("id", "123"); //QueryString is a class created to build query, not important to the question.
String url = "http://localhost:8080/webservices/webapi/activities/activity?" + qs;

URL obj;
try {
obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestProperty("Content-Type","application/json");
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla 5.0");
con.setDoOutput(true);

String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

Activity activity = GSON.fromJson(response.toString(), Activity.class); //This is for checking if i'm getting correct data back which I'm sending.

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


Method 2 - using WebTarget available via Jersey

private static void doPost(){
Response entity = webTarget
.path("activities/postactivity")
.queryParam("id",2204)
.queryParam("description","Foo")
.queryParam("duration",100)
.queryParam("name", "Bar")
.request()
.post(null);

}


Realistically these are about the same. WebTarget is a standard part of JAX-RS so if you were to move to another provider like RestEasy you wouldn't have to port it. If you like standards then this would be the way to go.

HttpClient is common though not a standard in the JSR way. It is very powerful and sometimes easier to use when you've got specialized requirements like custom SSL processing. It is, however, a pain to keep up with. Every release seems to deprecate something I'm using.

The security of either will depend on you - the libraries will not be significantly different in this area and it will be up to you to implement the code well.

Performance is also likely to be similar. It's highly likely that the performance of your system will be dependent on something that takes significantly more time than any differences in these libraries.

I would always vote for code that is more readable and I'd argue the WebTarget way is easier to read.