このチュートリアルでは、最後に作成したRESTサービスに「
GET
」と「
POST
」リクエストを実行するために、http://www.jboss.org/resteasy[RESTEasyクライアントフレームワークでRESTful Javaクライアントを作成する方法を示します”リンク://webservices/jax-rs/integrate-jackson-with-resteasy/[Jackson JAX-RS]”チュートリアル。
1. RESTEasyクライアントフレームワーク
RESTEasyクライアントフレームワークはRESTEasyコアモジュールに含まれているので、単に ”
resteasy-jaxrs.jar
“を `pom.xml`ファイルに宣言する必要があります。
File: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;
}
//...
RESTEasyクライアントは「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();
}
//...
RESTEasyクライアントは “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].... === ソースコードをダウンロードする ダウンロードする - リンク://wp-content/uploads/2011/07/RESTful-Java-Client-RESTEasyt-Example.zip[RESTful-Java-Client-RESTEasyt-Example.zip](9 KB) === 参考文献 . http://jackson.codehaus.org/%20[Jackson Official Website] . リンク://webservices/jax-rs/restfull-java-client-with-java-net-url/[RESTful Javaクライアントとjava.net.URL]。 link://webservices/jax-rs/restful-java-client-with-apache-httpclient/[RESTful JavaクライアントとApache HttpClient] link://tag/client/[client]link://tag/jax-rs/[jax-rs]リンク://タグ/resteasy/[resteasy]link://タグ/restful/[restful]