本教程将向您展示如何使用RESTEasy客户端框架创建RESTful Java客户端 ,以执行对上一个“ Jackson + JAX-RS ”教程中创建的REST服务的“ GET ”和“ POST ”请求。
1. RESTEasy客户端框架
RESTEasy核心模块中包含RESTEasy客户端框架,因此,您只需要在pom.xml文件中声明“ resteasy-jaxrs.jar ”。
档案:pom.xml
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.2.1.GA</version>
</dependency>
2. GET请求
查看上一个REST服务。
@Path("/json/product")
public class JSONService {
@GET
@Path("/get")
@Produces("application/json")
public Product getProductInJSON() {
Product product = new Product();
product.setName("iPad 3");
product.setQty(999);
return product;
}
//...
方便客户端发送“ GET”请求。
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.client.ClientProtocolException;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
public class RESTEasyClientGet {
public static void main(String[] args) {
try {
ClientRequest request = new ClientRequest(
"http://localhost:8080/RESTfulExample/json/product/get");
request.accept("application/json");
ClientResponse<String> response = request.get(String.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
new ByteArrayInputStream(response.getEntity().getBytes())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出…
Output from Server ....
{"qty":999,"name":"iPad 3"}
3. POST请求
还查看最后的REST服务。
@Path("/json/product")
public class JSONService {
@POST
@Path("/post")
@Consumes("application/json")
public Response createProductInJSON(Product product) {
String result = "Product created : " + product;
return Response.status(201).entity(result).build();
}
//...
方便客户端发送“ POST”请求。
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
public class RESTEasyClientPost {
public static void main(String[] args) {
try {
ClientRequest request = new ClientRequest(
"http://localhost:8080/RESTfulExample/json/product/post");
request.accept("application/json");
String input = "{\"qty\":100,\"name\":\"iPad 4\"}";
request.body("application/json", input);
ClientResponse<String> response = request.post(String.class);
if (response.getStatus() != 201) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
new ByteArrayInputStream(response.getEntity().getBytes())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出…
Output from Server ....
Product created : Product [name=iPad 4, qty=100]
下载源代码
下载它– RESTful-Java-Client-RESTEasyt-Example.zip (9 KB)
参考文献
翻译自: https://mkyong.com/webservices/jax-rs/restful-java-client-with-resteasy-client-framework/
本教程介绍如何利用RESTEasy客户端框架进行RESTful Java客户端开发,详细讲解了执行GET和POST请求的方法,并提供了源代码下载。

97

被折叠的 条评论
为什么被折叠?



