我需要使用angularjs应用程序的ajax请求从webservice中检索一些数据。由于CORS issue,我无法直接执行此操作,例如您可以看到此问题No 'Access-Control-Allow-Origin' header is present on the requested resource
据我所知,我唯一的解决方案是从我的服务器向第三方服务器发出请求,然后将其从我的服务器返回给我的客户端,如下图所示:
client -> my server -> 3rd party server -> my server -> client
。
直接的解决方案是使用JAX-RS在我的客户端和我的服务器以及Apache Http Client库之间进行通信,以便在我的服务器和第三方服务器之间进行通信,但我怀疑是否存在仅基于工具的解决方案JAX-RS提供给我?
答案 0 :(得分:1)
好的,我正在使用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();
}