Camel JAX-RS 和跨域请求 [英] Camel JAX-RS and Cross Domain Request
问题描述
我希望能够在我的本地 Camel 实例上执行 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.
我已经搜索过如何告诉 Camel 允许此类请求,但没有找到答案.我正在使用camel-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;
}
}
比标准路线更有效.
我如何告诉 Camel(或 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;
}
}
我认为您不需要新的依赖项,因为注释在camel-cxf 中.但是你需要告诉 Camel 去寻找这个注解,使用 providers 标签.
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).
这篇关于Camel JAX-RS 和跨域请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!