【问题标题】:Handle JSON request with Apache Camel使用 Apache Camel 处理 JSON 请求
【发布时间】:2016-01-13 16:15:30
【问题描述】:

我想处理第 3 方对我的系统执行的 HTTPs 请求。请求包含一个 JSON 对象

我曾尝试使用 Camel 的 Jetty 组件,但是当第 3 方执行请求时,Camel 会抛出以下异常:

org.apache.camel.RuntimeCamelException: Cannot read request parameters due Invalid parameter, expected to be a pair but was {"id":"321","rec":"533","status":"ok"}

Jetty 端点在蓝图中定义为:

<camel:endpoint id="getRequestEndpoint" uri="jetty:{{webServiceProtocol}}://{{jettyIp}}:{{jettyPort}}/getRequest?sslContextParametersRef=sslContextParameters"/>

我是错过了什么还是走错了路?

【问题讨论】:

  • 您是否尝试过在没有 SSL 的情况下让它工作?通常最好在不使用 SSL 的情况下进行测试,然后添加 HTTPS 部分,这样至少您知道其他一切都正常工作。

标签: java json jetty apache-camel


【解决方案1】:

确保您的客户端正在为 JSON 发送适当的 Content-Type HTTP 请求标头,即:

Content-Type: application/json

看起来它没有发送,Jetty 组件回退到表单数据解释。

【讨论】:

    【解决方案2】:

    确保您还将预期的 bean 类型添加到端点定义中,如下所示:

    .post()
        .type(User.class)
        .to("direct:your-queue")
    

    严格来说,这不是答案,因为在这种情况下,您会得到一个不同的异常,如下所示。但我希望它对某人有所帮助。

    org.apache.camel.InvalidPayloadException: No body available of type: 
    com.example.User but has value: {id=69, name=Hello world} of type: 
    java.util.LinkedHashMap
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多