【问题标题】:Consuming & exposing a REST service and passing JSON body使用和公开 REST 服务并传递 JSON 正文
【发布时间】:2020-04-07 06:33:40
【问题描述】:

我正在使用带有 XML DSL 的 Apache Camel 来使用和公开 REST 服务。 我有一个请求正文{"name":"your name","phonenumber":"111-111"}

我成功地能够直接发送此 json 请求正文并从使用的端点 url 获得成功的响应。然而,当我浏览暴露的 URL 时,我得到了以下异常。

org.apache.camel.component.restlet.RestletOperationException: Restlet operation failed invoking 
<!--Consumed URL--> with statusCode: 400 /n responseBody:
{"timestamp":"2020-04-07T06:15:41.302+0000","status":400,"error":"Bad Request",
"message":"Required request body is missing: public boolean
com.agcs.cids.BookingRestController.createBooking(org.bson.Document...)","path":"-----"}

我什至能够在调用 URL 之前成功打印正文。请在下面完善我的代码:

<log loggingLevel="TRACE" message="Request from source: Body: ${body}" />      
<log loggingLevel="TRACE" message="Request from source: Header: ${headers}" />
<to uri="restlet:<--Consumed URL-->?restletMethod=POST" />

【问题讨论】:

    标签: java rest apache-camel spring-camel


    【解决方案1】:

    我猜你的代码试图要么 -

    1. 发送不带{"name":"your name","phonenumber":"111-111"}JSON 正文的POST 请求。在这种情况下,您可能想重新访问您的代码并验证您是否正确设置了正文,例如 &lt;setBody&gt;&lt;to&gt;&lt;from&gt; 标记中正确使用。
    2. 向不接受JSON 正文的URL 发送POST 请求。您是否尝试将 "Accept", "application/json" 添加为标题?
    3. 验证您是否没有混淆您的 &lt;from&gt;&lt;to&gt; 地址(您提到了“使用的 URL”,在 &lt;to&gt; 端点的情况下,语义上应该是“使用的 URL”)

    【讨论】:

    • 1.我可以在调用 POST 请求之前打印正文。但是我是否有任何验证器来检查正文是否是有效的 JSON? 2. 我尝试将 appliccation/json 添加为内容类型并接受我没有运气。
    猜你喜欢
    • 2013-04-04
    • 2016-11-28
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    相关资源
    最近更新 更多