简体   繁体   English

使用 Apache Camel 处理 JSON 请求

[英]Handle JSON request with Apache Camel

I want to handle an HTTPs request that a 3rd party is doing towards my system.我想处理第 3 方对我的系统所做的 HTTPs 请求。 The requests contains a JSON object请求包含一个 JSON 对象

I have tried using the Jetty component of Camel but when the 3rd party performs the requests Camel throws the following exception:我曾尝试使用 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"}

The Jetty endpoint is defined in blueprint as: Jetty 端点在蓝图中定义为:

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

Am I missing something or are in a totally wrong path?我是否遗漏了什么或走在完全错误的道路上?

Make sure that your client is sending the appropriate Content-Type HTTP request header for JSON, which is:确保您的客户端正在为 JSON 发送适当的Content-Type HTTP 请求标头,即:

Content-Type: application/json

It looks like it wasn't sent and the Jetty component falls back to form data interpretation.看起来它没有被发送,Jetty 组件回退到表单数据解释。

Make sure you also add expected bean type to your endpoint definition, like this:确保您还将预期的 bean 类型添加到端点定义中,如下所示:

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

This is strictly speaking not an answer, because in this case you get a different exception shown below.严格来说,这不是答案,因为在这种情况下,您会得到如下所示的不同异常。 But I hope it helps someone.但我希望它可以帮助某人。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM