【问题标题】:how to send HTTP Post request using jersey with a complex parameter如何使用带有复杂参数的球衣发送 HTTP Post 请求
【发布时间】:2013-10-04 03:58:56
【问题描述】:

我需要使用以下复杂类型作为参数向 REST API 发送 HTTP Post。我查看了 jersey 的文档,它只有助于发送一个键值对。如何使用 jersey 发送具有以下参数的 HTTP Post 请求。

{
    "key": "example key",
    "message": {
        "html": "<p>Example HTML content</p>",
        "text": "Example text content",
        "subject": "example subject",
        "from_email": "message.from_email@example.com",
        "from_name": "Example Name",
        "to": [
            {
                "email": "recipient.email@example.com",
                "name": "Recipient Name"
            }
        ],
        "headers": {
            "Reply-To": "message.reply@example.com"
        },
        "important": false,
        "track_opens": null,
        "track_clicks": null,
        "auto_text": null,
        "auto_html": null,
        "inline_css": null,
        "url_strip_qs": null,
        "preserve_recipients": null,
        "view_content_link": null,
        "bcc_address": "message.bcc_address@example.com",
        "tracking_domain": null,
        "signing_domain": null,
        "return_path_domain": null,
        "merge": true,
        "global_merge_vars": [
            {
                "name": "merge1",
                "content": "merge1 content"
            }
        ],
        "merge_vars": [
            {
                "rcpt": "recipient.email@example.com",
                "vars": [
                    {
                        "name": "merge2",
                        "content": "merge2 content"
                    }
                ]
            }
        ],
        "tags": [
            "password-resets"
        ],
        "subaccount": "customer-123",
        "google_analytics_domains": [
            "example.com"
        ],
        "google_analytics_campaign": "message.from_email@example.com",
        "metadata": {
            "website": "www.example.com"
        },
        "recipient_metadata": [
            {
                "rcpt": "recipient.email@example.com",
                "values": {
                    "user_id": 123456
                }
            }
        ],
        "attachments": [
            {
                "type": "text/plain",
                "name": "myfile.txt",
                "content": "ZXhhbXBsZSBmaWxl"
            }
        ],
        "images": [
            {
                "type": "image/png",
                "name": "IMAGECID",
                "content": "ZXhhbXBsZSBmaWxl"
            }
        ]
    },
    "async": false,
    "ip_pool": "Main Pool",
    "send_at": "example send_at"
}

我查看了使用 Jersey 发送 HTTP Post 的其他问题,我能找到的只是一种只发送 key\value 对作为参数而不是像上面那样复杂的字符串类型的方法。

【问题讨论】:

    标签: java rest jersey jersey-client


    【解决方案1】:

    您应该看看 JAXB,它允许您从“对象”中“自动”构建“复杂参数”。基本上,该过程将定义一个类,该类表示您作为 REST API 资源接受的请求消息呈现的数据结构,然后用您想要发布的数据填充它并发送它。在这个问题中,您可以找到有关如何准确执行此操作的更多详细信息:Can jersey clients POST a JAXB object to the server using JSON?

    【讨论】:

      猜你喜欢
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-11
      • 1970-01-01
      • 2015-07-22
      • 2019-12-04
      相关资源
      最近更新 更多