【发布时间】:2011-06-14 14:31:31
【问题描述】:
有什么方法可以使用 servlet 访问 RESTful Web 服务。我不想使用 Jersey 客户端?
编辑:如何在 url 中传递对象并确保编组/解组正确完成?
【问题讨论】:
标签: java web-services servlets
有什么方法可以使用 servlet 访问 RESTful Web 服务。我不想使用 Jersey 客户端?
编辑:如何在 url 中传递对象并确保编组/解组正确完成?
【问题讨论】:
标签: java web-services servlets
您可以使用 commons-httpclient 库 (http://hc.apache.org/httpcomponents-client-ga/) 向您的 REST 服务发出请求,并使用 gson (http://code.google.com /p/google-gson/) 将 java 对象序列化/反序列化为 JSON
【讨论】:
您可以编写自己的 HTTP 请求,例如使用HTTPURLConnection。在这里您可以设置请求方法并根据需要更改 URL 或/和正文,例如
URL url = new URL("http://www.example.com/resource");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
// etc
这样你就只是使用标准的java.net API。
【讨论】:
你不需要使用jersey客户端,它只是一个URL,你可以使用:
new URL("http://locationofservice").openConnection();
【讨论】: