【问题标题】:How can I access a Restful webservice using servlet?如何使用 servlet 访问 Restful Web 服务?
【发布时间】:2011-06-14 14:31:31
【问题描述】:

有什么方法可以使用 servlet 访问 RESTful Web 服务。我不想使用 Jersey 客户端?
编辑:如何在 url 中传递对象并确保编组/解组正确完成?

【问题讨论】:

    标签: java web-services servlets


    【解决方案1】:

    您可以使用 commons-httpclient 库 (http://hc.apache.org/httpcomponents-client-ga/) 向您的 REST 服务发出请求,并使用 gson (http://code.google.com /p/google-gson/) 将 java 对象序列化/反序列化为 JSON

    【讨论】:

      【解决方案2】:

      您可以编写自己的 HTTP 请求,例如使用HTTPURLConnection。在这里您可以设置请求方法并根据需要更改 URL 或/和正文,例如

         URL url = new URL("http://www.example.com/resource");
         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
         connection.setRequestMethod("GET");
        // etc
      

      这样你就只是使用标准的java.net API。

      【讨论】:

        【解决方案3】:

        你不需要使用jersey客户端,它只是一个URL,你可以使用:

        new URL("http://locationofservice").openConnection();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-04-01
          • 2017-03-26
          • 1970-01-01
          • 2019-05-28
          • 2018-01-30
          • 2012-09-20
          • 2011-12-13
          相关资源
          最近更新 更多