如何使用 ContainerRequestContext 从 HTTP 请求中获取 JSESSIONID?
How to get the JSESSIONID from a HTTP request using ContainerRequestContext?
我在请求中有以下 HTTP headers,我想从中提取 JSESSIONID
:
Accept=application/json
Accept-Encoding=gzip
deflate,Accept-Language=en-us
Connection=keep-alive
Content-Length=0
Content-Type=application/json
Cookie=JSESSIONID=ss0ox8w99o9142b73rssvc0r
Host=localhost:8080
User-Agent=Test_QA/34 CFNetwork/711.5.7 Darwin/14.5.0 (x86_64)
我正在使用 ContainerRequestContext
如下:
@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {
System.out.println("***HEADER VALUE**: " + requestContext.getHeaderString("Cookie"));
}
得到的结果为:
JSESSIONID=zv71od6l2fd41hv6yf0980khy
并且:
@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {
Map<String, javax.ws.rs.core.Cookie> clientCookie = requestContext.getCookies();
System.out.println("Client Cookie Map: " + clientCookie);
}
得到的结果为:
Clinet Cookie Map: {JSESSIONID=JSESSIONID=1of1x5u1s1l4hdxfg2azlep42}
从请求中提取 JSESSIONID
的最佳方法是什么?
退一步
首先,REST 和会话标识符在同一句话中听起来不太好。
REST 中的 S 表示 stateless 而不是 stateful。在 REST 应用程序中,会话状态必须由客户端管理,而不是由服务器管理。因此,不能有任何会话标识符。
有关详细信息,请查看 and here。
从 JAX-RS
中的 cookie 获取值
我认为您正在寻找以下解决方案:
Cookie cookie = requestContext.getCookies().get("JSESSIONID");
String value = cookie.getValue();
有关Cookie
, have a look at the documentation的更多信息。
我在请求中有以下 HTTP headers,我想从中提取 JSESSIONID
:
Accept=application/json
Accept-Encoding=gzip
deflate,Accept-Language=en-us
Connection=keep-alive
Content-Length=0
Content-Type=application/json
Cookie=JSESSIONID=ss0ox8w99o9142b73rssvc0r
Host=localhost:8080
User-Agent=Test_QA/34 CFNetwork/711.5.7 Darwin/14.5.0 (x86_64)
我正在使用 ContainerRequestContext
如下:
@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {
System.out.println("***HEADER VALUE**: " + requestContext.getHeaderString("Cookie"));
}
得到的结果为:
JSESSIONID=zv71od6l2fd41hv6yf0980khy
并且:
@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {
Map<String, javax.ws.rs.core.Cookie> clientCookie = requestContext.getCookies();
System.out.println("Client Cookie Map: " + clientCookie);
}
得到的结果为:
Clinet Cookie Map: {JSESSIONID=JSESSIONID=1of1x5u1s1l4hdxfg2azlep42}
从请求中提取 JSESSIONID
的最佳方法是什么?
退一步
首先,REST 和会话标识符在同一句话中听起来不太好。
REST 中的 S 表示 stateless 而不是 stateful。在 REST 应用程序中,会话状态必须由客户端管理,而不是由服务器管理。因此,不能有任何会话标识符。
有关详细信息,请查看
从 JAX-RS
中的 cookie 获取值我认为您正在寻找以下解决方案:
Cookie cookie = requestContext.getCookies().get("JSESSIONID");
String value = cookie.getValue();
有关Cookie
, have a look at the documentation的更多信息。