2012-01-04 20 views
1

POSTを使用してREST Webサービスにラッパーオブジェクトを渡すことは可能ですか?REST:クライアントがPOSTを通じてラッパーオブジェクトを渡すことはできますか?

+2

私はあなたが_wrapperのOBJECT_で何を意味するかわからないが、サーバーはそのラッパーオブジェクトについて知る必要があります。オブジェクトを逆シリアル化する方法がわからないため、オブジェクトを作成してサーバーに送信することはできません。 – Thomas

+1

私はこの質問はあなたを助けるかもしれないと思う: http://stackoverflow.com/questions/1071749/how-to-reuse-jerseys-json-jaxb-for-serialization –

答えて

1

はいあなたはラッパーOject(クラスのオブジェクト)を渡すことができます。ジャージーレストクライアントの

例:
追加の依存関係:

  <!-- 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> 

ForGetMethodと渡す2つのパラメータ:

  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は一つのパラメータを渡すと、リストのRespone行き方:

 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として受け取り、それをJson Arrayに変換し、次にJson ArrayをListに変換するListを返します。

POSTリクエストをパラメータとしてJSONオブジェクトを渡した場合:

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 ")); 
    } 

ここでユーザーは、ユーザークラスのオブジェクトです。これはポストパラメータで実行されます。

関連する問題