简体   繁体   English

泽西岛WebTarget vs HTTPURLConnection

[英]Jersey WebTarget vs HTTPURLConnection

I'm learning web services using Java. 我正在使用Java学习Web服务。 I'm technically a noob, this is the code I've written, which works, I just don't know which method has what advantage over another, like which one is more secure? 从技术上讲,我是一个菜鸟,这是我编写的代码,可以正常工作,我只是不知道哪种方法相对于另一种方法有什么优势,例如哪种方法更安全? Which one will be more faster? 哪一个会更快? I'm not asking for complete too broad an answer. 我不是在要求完整的答案。 A short concise one will do. 简短的说明就可以了。 I've created a REST service using Jersey 2.x and I've created client to consume the said REST service. 我使用Jersey 2.x创建了REST服务,并创建了使用上述REST服务的客户端。

POST resource is as follows, POST资源如下

@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. 现在,我创建了一个Java应用程序客户端。 I'm consume the above REST Service above in following two ways. 我通过以下两种方式使用上述REST服务。

Method 1 Using HTTPURLConnection 方法1: 使用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 方法2: 使用通过Jersey提供的WebTarget

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);
    }

Thanks. 谢谢。

Honestyl i have two things to write to you: 1. The HttpURLConnection is the Java personal way of doing a retrieve of a web affiliate (like web services) but you have a better and stress-free way of doing it with Jersey and this will make things faster and smoother for you. Honestyl我有两件事要写给您:1. HttpURLConnection是Java个人方式来检索Web会员(如Web服务),但是您可以使用Jersey来实现更好而轻松的方式,这将使事情变得更快,更流畅。 For some persons they even say that the Jersey style is the High-level API while the HttpURLConnection is called the low-level API. 对于某些人,他们甚至说Jersey风格是高级API,而HttpURLConnection被称为低级API。 2. Your question was able to provide me with a necessary solution to a problem i have had for the past two days with consuming a @Queryparam POST webmethod. 2.您的问题能够为我提供过去两天因使用@Queryparam POST网络方法而遇到的问题的必要解决方案。 I really appreciate this. 我真的很感激。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM