骆驼JAX-RS和跨域请求 [英] Camel JAX-RS and Cross Domain Request

查看:608
本文介绍了骆驼JAX-RS和跨域请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够做到在我的本地骆驼例如HTTP请求(只为发展宗旨,我知道这是不好的做法)。现在,我坚持了:

I'd like to be able to do HTTP requests on my localhost Camel instance (just for development purpose, I know this is bad practice). For now, I'm stuck with :

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

我搜索我怎么能告诉骆驼允许这样的请求,但没有找到答案。我使用骆驼CXF和rsServer创建我的终点。

I've search how can I tell Camel to allow such requests, but didn't find an answer. I'm using camel-cxf and the rsServer to create my endpoint.

我有一个端点看起来就像是:

I've got an endpoint looking like that :

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

比标准路由做的工作。

Than a standard route is doing the job.

如何知道骆驼(或JAX-RS,或者CXFRS组成部分,我不知道...),以允许跨域请求?

How can I tell Camel (or JAX-RS, or the CXFRS component, I don't know...) to allow Cross Domain Requests ?

推荐答案

您需要在您的端点添加注释

You need to add an annotation on your endpoint

@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;
    }
}

我不认为你需要一个新的依赖,作为注释是骆驼CXF。但是,你需要告诉骆驼寻找这个注释,用供应商的标签。

I don't think you need a new dependency, as the annotation is in camel-cxf. But you need to tell Camel to look for this annotation, using the providers tag.

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

那么你应该罚款(但请记住,这只是本地测试)。

Then you should be fine (but remember that is just for local testing).

这篇关于骆驼JAX-RS和跨域请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆