cross-domain - Camel JAX-RS 和跨域请求

标签 cross-domain cxf jax-rs apache-camel cxfrs

我希望能够在我的本地 Camel 实例上执行 HTTP 请求(仅用于开发目的,我知道这是不好的做法)。现在,我坚持:

 Origin http://localhost:8000 is not allowed by Access-Control-Allow-Origin.

我搜索过如何让 Camel 允许此类请求,但没有找到答案。我正在使用 camel-cxf 和 rsServer 来创建我的端点。

我有一个看起来像这样的端点:

public class LoginEndpoint {
    @GET
    @Path(LOGIN)
    @Produces(MediaType.APPLICATION_JSON)
    public Customer login(@QueryParam("email") String email, @QueryParam("password") String password) {
        return null;
    }
}

比标准路线更有效。

我如何告诉 Camel(或 JAX-RS,或 CXFRS 组件,我不知道...)允许跨域请求?

最佳答案

您需要在端点上添加注释

@CrossOriginResourceSharing(allowAllOrigins = true, allowAnyHeaders = true)
public class LoginEndpoint {
    @GET
    @Path(LOGIN)
    @Produces(MediaType.APPLICATION_JSON)
    public Customer login(@QueryParam("email") String email, @QueryParam("password") String password) {
        return null;
    }
}

我认为您不需要新的依赖项,因为注释在 camel-cxf 中。但是您需要使用 providers 标签告诉 Camel 查找此注释。

<cxf:rsServer id="login" address="your adress"
        serviceClass="LoginEndpoint">
    <cxf:providers>
        <bean class="org.apache.cxf.jaxrs.cors.CrossOriginResourceSharingFilter" />
    </cxf:providers>
</cxf:rsServer>

那么你应该没问题(但请记住,这只是用于本地测试)。

关于cross-domain - Camel JAX-RS 和跨域请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12526724/

相关文章:

ruby-on-rails - Ruby on Rails如何获取当前URL?

jquery - 对 WCF Restful 服务的 Json 调用 (jquery) 始终运行错误函数

java - CXF BusException 命名空间 http ://cxf. 没有 DestinationFactory apache.org/transports/http

java - 使用 REST webservice 上传文件是个好主意吗?

java - JAX-RS JAXB Jackson 不使用@XmlRootElement 名称

javascript - SCORM: SecurityError: Blocked a frame with origin from access a cross-origin frame FOR SAME ORIGIN

jquery - Opera中使用jQuery的ajax(跨域请求)

java - Jersey 2.4 和 CXF 2.6

maven - 使用 cxf jar 打包 Camel 项目

java Holder vs document-style,或者为什么要使用Holders