2010-12-04 82 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));