首页 > 解决方案 > 使用和公开 REST 服务并传递 JSON 正文

问题描述

我正在使用带有 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" />

标签: javarestapache-camelspring-camel

解决方案


我猜你的代码试图要么 -

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

推荐阅读