2010-12-04 83 views
3

我有一個服務,期望2個對象...身份驗證和客戶端。 兩者都映射正確。Resteasy服務期待2 json對象

我想把它們當作Json來使用,但我很難做到這一點。 如果我只指定一個參數,它可以正常工作,但我怎樣才能調用這個服務傳遞2個參數?總是給我一些例外。

這裏是我的休息服務:

@POST 
@Path("login") 
@Consumes("application/json") 
public void login(Authentication auth, Client c) 
{ 
    // doing something 
} 

這裏是我的PHP消費者:

$post[] = $authentication->toJson(); 
$post[] = $client->toJson(); 

$resp = curl_post("http://localhost:8080/login", array(), 
      array(CURLOPT_HTTPHEADER => array('Content-Type: application/json'), 
        CURLOPT_POSTFIELDS => $post)); 

我想什麼一些變化穿上CURLOPT_POSTFIELDS太多,但不能讓它開始工作。

+0

你如何將它們轉換爲JSON?嘗試使用$ post ['Authentication'] = $ authentication-> toJson(); $ post ['Client'] = $ client-> toJson(); – Baba 2012-02-25 11:43:13

回答

0

您可能遇到的問題是您將$ post聲明爲一個編號數組,它可能包含您映射的數組鍵。基本上,這就是你給它:

Array(
    1 => Array(
      'authentication' => 'some data here' 
    ), 
    2 => Array(
      'client' => 'some more data here' 
    ) 
) 

當在現實中,你應該創建$後VAR像這樣:

Array(
    'authentication' => 'some data here', 
    'client' => 'some more data here' 
) 

試着改變你的代碼,以更多的東西像這樣(不最佳,但應該完成工作):

$authentication = $authentication->toJson(); 
$client = $client->toJson(); 
$post = array_merge($authentication, $client); 

$resp = curl_post("http://localhost:8080/login", array(), 
     array(CURLOPT_HTTPHEADER => array('Content-Type: application/json'), 
       CURLOPT_POSTFIELDS => $post));