使用 jax-rs 重定向获取请求和 return 响应来解决 CORS 问题?

Using jax-rs to redirect get request and return response to solve CORS issue?

我需要使用来自 angularjs 应用程序的 ajax 请求从网络服务中检索一些数据。由于 CORS issue, for example you can see this question No 'Access-Control-Allow-Origin' header is present on the requested resource

,我不能直接这样做

据我所知,我唯一的解决方案是从我的服务器向第 3 方服务器执行请求,然后 return 从我的服务器向我的客户端执行请求,如下图所示: client -> my server -> 3rd party server -> my server -> client.

直接的解决方案是使用 JAX-RS 在我的客户端和我的服务器之间进行通信,并使用 Apache Http Client 库在我的服务器和第 3 方服务器之间进行通信,但我怀疑是否有仅基于JAX-RS 为我提供了哪些工具?

好的,我使用的是 JAX-RS 2.0,它有自己的客户端,所以很简单:

@GET
public Response getRedirectedRequest(@QueryParam("query") String query){

    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://example.com")
                                    .path("/path/api/")
                                    .queryParam("q", query);

    Response response = target.request()
            .accept("application/json; charset=utf-8")
            .get();

    String json = response.readEntity(String.class);
    response.close();

    return Response.ok(json).build();
}