gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:59:27 26 4
gpt4 key购买 nike

我希望能够在我的本地 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com