问题描述
我在JAX-RS API中找到两个类 javax.ws.rs.core.Cookie
和 javax.ws.rs.core.NewCookie
。一个比另一个的优点是什么?
I found two classes in JAX-RS API javax.ws.rs.core.Cookie
and javax.ws.rs.core.NewCookie
. What are the advantages of one over another?I would like to know Which one is recommended to use and when?
提前感谢:)
推荐答案
这不是关于推荐的,它是关于适当。一个用于请求,一个用于响应。您可以看到两个不同的javadoc。
It's not about recommended, it's about appropriate. One is for a request, and one is for a response. You can see the two different javadocs.
在 Response
中发送时,$ c $ b
NewCookie
将设置 Set-Cookie 标头与cookie信息,
Cookie
将设置 Cookie
请求标题。这是根据HTTP规范。
NewCookie
, when sent in the Response
, will set a Set-Cookie
response header with the cookie information, and Cookie
will set the Cookie
request header with the cookie information. This is per the HTTP spec.
使用示例:
@GET
public Response get() {
return Response.ok("blah").cookie(new NewCookie("foo", "bar")).build();
}
[..]
Client client = ClientBuilder.newClient();
Response response = client.target(url).request().cookie(new Cookie("foo", "bar")).get();
这篇关于javax.ws.rs.core.Cookie vs javax.ws.rs.core.NewCookie,有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!