Jersey jax.rs客户端2.5从HTTP重定向到HTTPS [英] Jersey jax.rs client 2.5 follow redirect from HTTP to HTTPS

查看:91
本文介绍了Jersey jax.rs客户端2.5从HTTP重定向到HTTPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设置,其中托管我的REST服务器的tomcat服务器将呼叫从HTTP(端口9080)重定向到HTTPS(端口9443).

I have a setup where tomcat server hosting my REST servers redirects calls from HTTP (port 9080) to HTTPS ( port 9443 ).

我使用的是jersey 2.5实现,无法配置客户端以进行重定向.

I'm using jersey 2.5 implementation and cannot manage to configure the clients to follow redirections.

我发现了这个问题( Jersey客户端未遵循重定向 ),但为jersey 1.X系列提供了此功能,并且API发生了更改.

I've found this SO question( Jersey client doesn't follow redirects ), however it's provided for jersey 1.X series and the API has changed.

我尝试使用以下测试代码将其改编为2.5:

I've tried to adapt it for 2.5 with the following test code :

 SSLContextProvider ssl = new TrustAllSSLContextImpl(); // just trust all certs
 Response response  =     ClientBuilder.newBuilder()
     .sslContext(ssl.getContext()).newClient()
     .register(LoggingFilter.class)
     .target("http://testhost.domain.org:9080/rest.webapp/api/v1/hello/")
     .property(ClientProperties.FOLLOW_REDIRECTS, Boolean.TRUE)
     .request().get();   
 Assertions.assertThat(response.getStatus()).isNotEqualTo(302);

哪个失败,因为客户端似乎不遵循重定向.这是日志记录过滤器提供的内容:

Which fails as the client does not seem to follow the redirections. Here is what the logging filter provides :

Feb 14, 2014 12:23:45 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Sending client request on thread main
1 > GET http://testhost.domain.org:9080/rest.webapp/api/v1/hello/

Feb 14, 2014 12:23:45 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Client response received on thread main
1 < 302
1 < Cache-Control: private
1 < Content-Length: 0
1 < Date: Fri, 14 Feb 2014 11:38:59 GMT
1 < Expires: Thu, 01 Jan 1970 01:00:00 CET
1 < Location: https://testhost.domain.org:9443/rest.webapp/api/v1/hello/
1 < Server: Apache-Coyote/1.1

从球衣文档中,我了解到,需要做的就是将ClientProperties.FOLLOW_REDIRECTS属性添加到客户端,但这显然不是这种情况.我还发现了一些消息,表明可能是客户端过滤器需要进行重定向,但未找到任何示例或准则.

From the jersey doc I understood that all that needs to be done is to add the ClientProperties.FOLLOW_REDIRECTS property to the client, but this obviously not to be the case.I've also found messages indicating that maybe a client side filter is required to follow the redirections, but found no examples or guidelines on this.

因此,如果任何对jax.rs和重定向有一定经验的人可以将我引向一些指导/文档/示例代码,我将不胜感激.

So if anyone having some experience with jax.rs and redirection could point me to some directions/docs/example code I'd really appreciate.

推荐答案

执行此操作的正确方法是:

correct way to do this is:

webTarget.property(ClientProperties.FOLLOW_REDIRECTS, Boolean.TRUE);

这篇关于Jersey jax.rs客户端2.5从HTTP重定向到HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆