消费并暴露 REST 服务并传递 JSON 主体

huangapple 未分类评论47阅读模式
英文:

Consuming & exposing a REST service and passing JSON body

问题

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

我能够成功地直接发送这个JSON请求正文,并从消费的端点URL获得成功的响应。然而,当我通过我的暴露的URL进行访问时,我得到以下异常。

    org.apache.camel.component.restlet.RestletOperationException: 调用Restlet操作失败 
    <!--消费的URL--> 状态码:400 /n 响应正文:
    {"timestamp":"2020-04-07T06:15:41.302+0000","status":400,"error":"Bad Request",
    "message":"缺少必需的请求正文:public boolean
    com.agcs.cids.BookingRestController.createBooking(org.bson.Document...)","path":"-----"}

我甚至成功地能够在调用URL之前打印出请求正文。请查看我的代码如下:

    <log loggingLevel="TRACE" message="来自源的请求:正文:${body}" />      
    <log loggingLevel="TRACE" message="来自源的请求:头部:${headers}" />
    <to uri="restlet:<--消费的URL-->?restletMethod=POST" />
英文:

I am using Apache Camel with XML DSL to consume & expose a REST service.
I have a request body {"name":"your name","phonenumber":"111-111"}

I am successfully able to directly send this json request body and get a successfull response from the consumed endpoint url. Whereas when I go through my exposed URL I am getting the following exception.

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":"-----"}

I am successfully even able to print the body just before I make a call to the URL. Please fine my code below:

<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" />

答案1

得分: 0

我猜你的代码要么是在尝试发送一个 POST 请求,但没有包含 {"name":"your name","phonenumber":"111-111"} 这个 JSON 主体。在这种情况下,你可能需要检查一下你的代码,验证你是否正确地设置了请求主体,比如 <setBody> 是否在 <to><from> 标签内部正确使用了。

要么是在尝试向一个不接受 JSON 主体的 URL 发送 POST 请求。你是否尝试将 "Accept", "application/json" 添加为一个头部?

还要验证一下你的 <from><to> 地址是否混淆了(你提到了 "Consumed URL",在 <to> 端点的情况下,语义上应该是 "Consuming URL")。

英文:

I guess your code is trying to either -

  1. Send in a POST request without the {"name":"your name","phonenumber":"111-111"}JSON body. In this case, you might wanna revisit your code and verify if you're setting the body correctly, like the <setBody> is properly used inside the <to> and <from> tags.
  2. Send in a POST request to a URL which doesn't accept a JSON body. Have you tried adding "Accept", "application/json" as a header?
  3. Verify if you don't have your <from> and <to> addresses mixed up (you mentioned "Consumed URL" which semantically should be "Consuming URL" in case of a <to> endpoint)

huangapple
  • 本文由 发表于 2020年4月7日 14:33:40
  • 转载请务必保留本文链接:https://java.coder-hub.com/61074084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定