【问题标题】:Apache Camel Restlet - CORS IssueApache Camel Restlet - CORS 问题
【发布时间】:2016-01-21 18:13:37
【问题描述】:

这条路线有效,并且使用 SoapUI 可以很好地工作:

from("restlet:http://localhost:8484/restletTestService/submit?restletMethod=POST")
    .routeId("myRestletSubmitRoute")
    .unmarshal().json(JsonLibrary.Jackson, MyRequest.class)
    .to("bean:myServiceSubmitProcessor")
    .marshal().json(JsonLibrary.Jackson, MyResponse.class);

使用 SoapUI,我可以发布符合 MyRequest 类结构的 json,然后我会返回一个包含我认为应该包含的信息的 json。

但是,当我创建一个快速的 angularjs 页面以允许用户即时构建请求然后“POST”到我的休息端点时,嗯:

XMLHttpRequest cannot load http://localhost:8484/restletAddressService/addressPersonator/submit. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 405.

我很确定我必须以某种方式将标头 Access-Control-Allow-Origin 设置为 restlet URI 上的某个值,但我不知道该怎么做。我研究了文档 (http://camel.apache.org/restlet.html) 并用谷歌搜索了它,但我没有找到任何有用的东西。

有人知道答案吗?谢谢!

更新

因此,来自 fiw 的答案给了我所需的提示以及一些关于如何调用休息资源确定允许的内容的谷歌研究(加上相当多的老式试验和错误:))。这就是最终的工作:

from("restlet:http://localhost:8484/restletTestService/submit?restletMethod=POST")
    .routeId("myRestletSubmitRoutePOST")
    .unmarshal().json(JsonLibrary.Jackson, MyRequest.class)
    .to("bean:myServiceSubmitProcessor")
    .marshal().json(JsonLibrary.Jackson, MyResponse.class)
    .setHeader("Access-Control-Allow-Headers", constant("Content-Type"))
    .setHeader("Access-Control-Allow-Origin", constant("*"));

from("restlet:http://localhost:8484/restletTestService/submit?restletMethod=OPTIONS")
    .routeId("myRestletSubmitRouteOPTIONS")
    .setHeader("Access-Control-Allow-Headers", constant("Content-Type"))
    .setHeader("Access-Control-Allow-Origin", constant("*"));

谢谢!

【问题讨论】:

  • 在我的情况下,第二条路由没有终止调用 post-url,有什么解决方法吗?
  • Ur Update 帮助我解决了我的问题。谢谢:)

标签: apache-camel


【解决方案1】:

我认为您需要按照您的建议将 Access-Control-Allow-Origin 设置为响应的标头,其值为 *。所以要在骆驼中做到这一点:

from("restlet:http://localhost:8484/restletTestService/submit?restletMethod=POST")
    .routeId("myRestletSubmitRoute")
    .unmarshal().json(JsonLibrary.Jackson, MyRequest.class)
    .to("bean:myServiceSubmitProcessor")
    .marshal().json(JsonLibrary.Jackson, MyResponse.class)
    .setHeader("Access-Control-Allow-Origin", constant("*"));

这来自阅读:enable cors on your server

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    相关资源
    最近更新 更多