通过JSR 311及其实现,我们有了一个通过REST公开Java对象的强大标准。然而,在客户端,似乎缺少了一些类似于Apache Axis for SOAP的东西——隐藏web服务并将数据透明地封送回Java对象的东西。

如何创建Java RESTful客户端?使用HTTPConnection和手动解析结果?或者专门的客户端,例如Jersey或Apache CXR?


当前回答

尽管创建一个HTTP客户端并进行重新测试很简单。但是如果你想利用一些自动生成的客户端,你可以使用WADL来描述和生成代码。

您可以使用RestDescribe来生成和编译WSDL,也可以使用它生成php、ruby、python、java和c#中的客户端。它可以生成干净的代码,并且在代码生成之后,你需要对它进行一些调整,你可以在这里找到良好的文档和工具背后的潜在思想。

wintermute中提到了一些有趣而有用的WADL工具。

其他回答

泽西休息客户端的例子

添加依赖项:

<!-- Jersey -->
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.8</version>
</dependency>

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.8</version>
</dependency>
    
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.8</version>
</dependency>

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20090211</version>
</dependency>

对于GetMethod,并传递两个参数:

Client client = Client.create();
WebResource webResource1 = client
                        .resource("http://localhost:10102/NewsTickerServices/AddGroup/"
                                + userN + "/" + groupName);

ClientResponse response1 = webResource1.get(ClientResponse.class);
System.out.println("responser is" + response1);

GetMethod传递一个参数并获得一个List类型的响应:

Client client = Client.create();
 
WebResource webResource1 = client
                    .resource("http://localhost:10102/NewsTickerServices/GetAssignedUser/"+grpName);    
//value changed
String response1 = webResource1.type(MediaType.APPLICATION_JSON).get(String.class);
 
List <String > Assignedlist = new ArrayList<String>();
JSONArray jsonArr2 = new JSONArray(response1);
for (int i =0;i<jsonArr2.length();i++){
        
    Assignedlist.add(jsonArr2.getString(i));    
}

上面它返回一个列表,我们将其作为list对象接受,然后将其转换为JSONArray,然后从JSONArray转换为list。

Post请求传递JSONObject作为参数:

Client client = Client.create();
WebResource webResource = client
            .resource("http://localhost:10102/NewsTickerServices/CreateJUser");
// value added

ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, mapper.writeValueAsString(user));

if (response.getStatus() == 500) {

    context.addMessage(null, new FacesMessage("User already exist "));
}

你可以试试拉帕。让我们知道你的反馈。 并随时记录问题或预期的功能。

我大部分时间都使用RestAssured来解析rest服务响应和测试服务。除了Rest Assured之外,我还使用下面的库来与Resful服务进行通信。

a. Jersey Rest客户端

b. Spring RestTemplate

c. Apache HTTP Client

我目前正在使用https://github.com/kevinsawicki/http-request,我喜欢他们的简单性和展示示例的方式,但当我阅读时,我主要是被卖了:

依赖关系是什么? 一个也没有。这个库的目标是成为带有一些内部静态类的单个类。测试项目确实需要Jetty,以便根据实际的HTTP服务器实现测试请求。

这解决了Java 1.6项目中的一些问题。至于把json解码成对象,gson是无敌的:)

您可以使用标准的Java SE api:

private void updateCustomer(Customer customer) { 
    try { 
        URL url = new URL("http://www.example.com/customers"); 
        HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
        connection.setDoOutput(true); 
        connection.setInstanceFollowRedirects(false); 
        connection.setRequestMethod("PUT"); 
        connection.setRequestProperty("Content-Type", "application/xml"); 

        OutputStream os = connection.getOutputStream(); 
        jaxbContext.createMarshaller().marshal(customer, os); 
        os.flush(); 

        connection.getResponseCode(); 
        connection.disconnect(); 
    } catch(Exception e) { 
        throw new RuntimeException(e); 
    } 
} 

或者您可以使用JAX-RS实现(如Jersey)提供的REST客户机api。这些api更容易使用,但在类路径上需要额外的jar。

WebResource resource = client.resource("http://www.example.com/customers"); 
ClientResponse response = resource.type("application/xml");).put(ClientResponse.class, "<customer>...</customer."); 
System.out.println(response); 

有关更多信息,请参阅:

http://bdoughan.blogspot.com/2010/08/creating-restful-web-service-part-55.html