Camel JAX-RS and Cross Domain Request

妖精的绣舞 提交于 2019-12-05 19:21:44
cexbrayat

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

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).

If you are using CXF 3.1.9, it is found in cxf-rt-rs-security-cors jar.

<jaxrs:server id="apiRS" address="/api">
    <jaxrs:serviceBeans>
            <bean class="com.ak.util.APIRSController"></bean>
    </jaxrs:serviceBeans>
    <jaxrs:providers>
        <bean class="org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter" />
    </jaxrs:providers>
</jaxrs:server>

And REST

@CrossOriginResourceSharing(allowAllOrigins = true)
public class APIController {

    @GET
    @Path("/status")
    public String status() {
        return "{\"Result\":\"OK\"}";
    }
}

For more security see allowOrigins and other parameters at above annotation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!