0
\$\begingroup\$

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.
    qs.add("duration", "12");
    qs.add("description", "This is description");
    qs.add("name", "This is Name");
    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);


        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        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);
String entityRead = entity.readEntity(String.class);

System.out.println(entityRead);
Activity activityRead = GSON.fromJson(entityRead, Activity.class);
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.