【问题标题】:Resteasy service expecting 2 json objects需要 2 个 json 对象的 Resteasy 服务
【发布时间】:2010-12-04 17:10:14
【问题描述】:

我有一个需要 2 个对象的服务...身份验证和客户端。 两者都正确映射。

我试图将它们作为 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 上尝试了一些变化,但无法让它发挥作用。

【问题讨论】:

  • 您如何将它们转换为 JSON ?尝试使用 $post['Authentication'] = $authentication->toJson(); $post['Client'] = $client->toJson();

标签: php json service resteasy


【解决方案1】:

您可能遇到的问题是您将 $post 声明为编号数组,其中可能包含您要映射的数组键。基本上,这就是你给它的:

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

实际上,您应该像这样创建 $post 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));

【讨论】:

    猜你喜欢
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 2021-12-14
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多