【发布时间】:2014-03-13 18:10:12
【问题描述】:
我有一个设置,其中托管我的 REST 服务器的 tomcat 服务器将调用从 HTTP(端口 9080)重定向到 HTTPS(端口 9443)。
我正在使用 jersey 2.5 实现,无法配置客户端以遵循重定向。
我发现了这个 SO 问题 (Jersey client doesn't follow redirects),但是它是为 jersey 1.X 系列提供的,并且 API 已更改。
我尝试使用以下测试代码使其适应 2.5:
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);
由于客户端似乎没有遵循重定向而失败。以下是日志过滤器提供的内容:
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 属性添加到客户端,但这显然不是这种情况。我还发现消息表明可能是客户端过滤器需要遵循重定向,但没有找到这方面的示例或指南。
因此,如果有人对 jax.rs 和重定向有一定的经验,可以向我指出一些方向/文档/示例代码,我将不胜感激。
【问题讨论】:
标签: rest tomcat redirect jersey-2.0 jersey-client