Ste*_*fen 2 java collections type-conversion
如何将a转换java.util.Collections$UnmodifiableRandomAccessList为a Collections.singletonList?在尝试存储两个服务之间的会话时,我发现了这一点,但是我无法弄清楚两者之间的步骤。首先获取我需要设置的cookie信息:
Map<String, Collections> headerInfo = (Map<String, Collections>)
((BindingProvider) port).getResponseContext()
.get(MessageContext.HTTP_RESPONSE_HEADERS);
Run Code Online (Sandbox Code Playgroud)
现在,我可以获得所需的Cookie信息;如果我做一个
System.out.println(headerInfo.get("Set-Cookie"));
Run Code Online (Sandbox Code Playgroud)
我得到这样的东西
Set-Cookie=[PHPSESSID=rpsnc2g7o4ltbr6l9qus177p14; path=/];
Run Code Online (Sandbox Code Playgroud)
现在我只需要这样做:
((BindingProvider) port2).getRequestContext()
.put(MessageContext.HTTP_REQUEST_HEADERS,
Collections.singletonMap("Cookie", Collections.singletonList(cookieValue)));
Run Code Online (Sandbox Code Playgroud)
但我不知道如何到达
headerInfo.get("Set-Cookie"):cookieValue
这是我在Q:
JAX-WS客户端中 发现问题解决方案的第一部分的问题:跨多个服务维护会话/ cookie
(这可能也可以解释我的问题)
解决方案是通过转换为正确的类/接口来使用原始列表:
List<String>
Run Code Online (Sandbox Code Playgroud)
代替:
Collections
Run Code Online (Sandbox Code Playgroud)
工作了。
Map<String, List<String>> headers = (Map<String, List<String>>)((BindingProvider) authPort).getResponseContext().get(MessageContext.HTTP_RESPONSE_HEADERS);
List<String> setCookie = (List<String>) headers.get("Set-Cookie");
((BindingProvider) servicePort).getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS,Collections.singletonMap("Cookie", setCookie ));
Run Code Online (Sandbox Code Playgroud)