JAXRS 2.0 客户端:FOLLOW_REDIRECTS 属性 不起作用

JAXRS 2.0 Client: FOLLOW_REDIRECTS property doesn't work

我正在尝试让自动重定向在 Jersey Client 2.0 中工作。这是我的代码:

ClientConfig cc = new ClientConfig().property(ClientProperties.FOLLOW_REDIRECTS, true);
Client c = ClientBuilder.newClient(cc);
WebTarget wt = c.target("some_path");
SystemInfo info = wt.request(MediaType.APPLICATION_XML_TYPE).get(SystemInfo.class);

服务器按预期在位置 header 发送 HTTP 302 和另一个 URL。我假设按照Jersey JAXRS Client API the client will redirect automatically to the new specified URL, but I'm getting a RedirectionException代替。

这是适当的行为吗?在 try-catch-block?

中如何在不手动实施重定向的情况下使客户端重定向工作

提前致谢!


更新:

我找到了奇怪行为的问题点。如果以编程方式在服务器上使用类似以下内容进行重定向:

return Response.seeOther(another_uri).build();

一切都很好。但就我而言,重定向是由于部署描述符中的 security-constraint 元素造成的:

<security-constraint>
    ...
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>

因此,客户端由 servlet 容器自动从 http:// localhost:8080/some_path 重定向到 https:// localhost:8181/another_path。 在浏览器中它工作正常,但 Jersey 客户端似乎忽略 FOLLOW_REDIRECTS 属性 并抛出一个 RedirectionException。

在这种情况下是否有机会使重定向正常工作?谢谢!

经过一些调查,我发现 Jersey Client 不对问题负责。如果响应的 Location header 有一些其他方案,而不是起源 URL,那么它是一般的 Java HttpURLConnection 行为(或者更确切地说是安全限制)。

因此,如果请求以某种方式(响应代码 3xx)重定向到具有其他方案的资源,HttpURLConnection 将忽略 followRedirects 标志和 return 以及适当的响应代码。否则,如果 followRedirects=true,它会向建议的 URL 发送后续请求,并从该资源发送 return 的响应。

有关详细信息,请参阅 Java doesn't follow redirect in URLConnection