【Java】【REST】JavaでRESTRestクライアント を実装する ~Apache Commons を使用した場合~

Apache Commons を使用した場合

 * 下記の記事で暗号化する際に使用した Apache Commons を使って、Restを実装する
http://blogs.yahoo.co.jp/dk521123/32780473.html
 * ダウンロードや設定については、上記の記事を参照のこと。

サンプル

使用するWebサービス

 * Axis2で自作したサービス(例えば、以下のURLを参考)を起動させて、実行した
http://blogs.yahoo.co.jp/dk521123/31944955.html

RestClientWithApacheCommons.java

import java.io.IOException;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class RestClientWithApacheCommons {
   private static final boolean isGetMethod = true;
   
   public static void main(String[] args) {
      String url = "http://localhost:8080/SampleServerWithAxis2/services/HelloWorld/SayHello";
      HttpClient client = new HttpClient();
      HttpMethodBase method = null;

      try {
          if (RestClientWithApacheCommons.isGetMethod) {
              // GET
              url += "?name=mike";
              method = new GetMethod(url);
              HttpMethodParams httpMethodParams = method.getParams();
              httpMethodParams.setParameter(
                    HttpMethodParams.RETRY_HANDLER,
                      new DefaultHttpMethodRetryHandler(1, false));
          } else {
             // POST
              NameValuePair[] data = {
                    new NameValuePair("name", "mike"),
                    new NameValuePair("age", "21")};
              method = new PostMethod(url);
              method.getParams().setContentCharset("UTF-8");
              HttpMethodParams httpMethodParams = method.getParams();
              httpMethodParams.setParameter(
                    HttpMethodParams.RETRY_HANDLER,
                      new DefaultHttpMethodRetryHandler(1, false));
              ((PostMethod)method).setRequestBody(data);
          }

          // リクエスト実行
          int statusCode = client.executeMethod(method);

          // ステータスコード出力
          System.out.println("statusCode = " + statusCode);

          // ヘッダー出力
          Header[] headers = method.getResponseHeaders();
          for (Header header : headers) {
              System.out.print("Header : " + header);
          }

          // ボディー出力
          String responseBody = method.getResponseBodyAsString();
          System.out.println(responseBody);

          // フッター出力
          Header[] footers = method.getResponseFooters();
          for (Header footer : footers) {
             System.out.print("Footer : " + footer);
          }
      } catch (HttpException e) {
      } catch (IOException e) {
      } finally {
          method.releaseConnection();
      }
   }
}

出力

statusCode = 200
Header : Server: Apache-Coyote/1.1
Header : Content-Type: application/xml;charset=UTF-8
Header : Transfer-Encoding: chunked
Header : Date: Tue, 21 Jan 2014 12:58:53 GMT
<ns:SayHelloResponse xmlns:ns="Hello">http://services.demo">Hello mike</ns:return></ns:SayHelloResponse>


関連記事

JavaでRESTRestクライアント を実装する

Axis2 を使用した場合

http://blogs.yahoo.co.jp/dk521123/32790425.html

ライブラリ jersey-client を使用した場合

http://blogs.yahoo.co.jp/dk521123/32824085.html

HttpURLConnection を使用した場合

http://blogs.yahoo.co.jp/dk521123/32936064.html

Apache Commons を使用した場合

http://blogs.yahoo.co.jp/dk521123/32936814.html