WebFLux与WebMvc的差异
WebFlux读写Cookie不像WebMvc那么直接,最主要的原因是WebMvc是基于Servlet规范的,而WebFlux仅仅遵守的是HTTP协议。所以在使用的时候会发现HttpServletRequest、HttpServletResponse这些Servlet层级的接口根本就无法使用。
Cookie与Servlet并没有太直接的关系,前者是属于HTTP规范的而后者是一个J2EE的规范,在应用层面仅有的联系就是Servlet会读写Cookie中的JSESSIONID来标记与前端浏览器和服务端的关系。而HttpServletRequest、HttpServletResponse仅是Servlet为请求和响应提供header、body管理的接口。
WebFlux的Cookie管理
WebFlux目前并没有为写Cookie提供任何工具。这就需要开发者按照HTTP的规范来写Cookie。 在HTTP协议交互的过程中,服务端可以通过在response中添加Set-Cookie头来让浏览器记录Cookie,而浏览器则在request中使用Cookie头来传递cookie。
写Cookie
写cookie使用ResponseEntity向response头中添加Set-Cookie即可。CookieBuilder的代码比较长,它是用于构建一个cookie字符串,Set-Cookie头除了设置key=value,还可以设置过期日期expires,域名domain,路径path等。
@RestController
@RequestMapping("/cookie")
public class CookieReadAWriteController {
@GetMapping("/write")
public ResponseEntity<String> cookieWrite() {
HttpHeaders headers = new HttpHeaders();
String cookie = new CookieBuilder().setKey("cookie-text")
.setValue(cookieText)
.setMaxAge(840000)
.setPath("/")
.build();
headers.add("Set-Cookie", cookie);
return new ResponseEntity<String>("hi," + userName, headers, HttpStatus.OK);
}
}
class CookieBuilder {
private String key;
private String value;
private String expires;
private String domain;
private String path;
public CookieBuilder setKey(String key) {
this.key = key;
return this;
}
public CookieBuilder setValue(String value) {
this.value = value;
return this;
}
public CookieBuilder setMaxAge(long ms) {
//cookie的过期日期为GMT格式的时间。
Date date = new Date(new Date().getTime() + ms);
SimpleDateFormat sdf = new SimpleDateFormat("EEE d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
this.expires = sdf.format(date);
return this;
}
public CookieBuilder setDomain(String domain) {
this.domain = domain;
return this;
}
public CookieBuilder setPath(String path) {
this.path = path;
return this;
}
public String build() {
StringBuilder sb = new StringBuilder();
sb.append(this.key);
sb.append("=");
sb.append(this.value);
sb.append(";");
if (null != this.expires) {
sb.append("expires=");
sb.append(this.expires);
sb.append(";");
}
if (null != this.domain) {
sb.append("domain=");
sb.append(this.domain);
sb.append(";");
}
if (null != this.path) {
sb.append("path=");
sb.append(this.path);
sb.append(";");
}
return sb.toString();
}
}
读cookie
获取cookie就比较直观,可以直接使用@CookieValue这个Annotation来获取:
@RestController
@RequestMapping("/cookie")
public class CookieReadAWriteController {
@GetMapping("/read/annotation")
/**
* @param value
* @return
*/
public String cookieReadAnnotation(@CookieValue("cookie-text") String value) {
return "当前Cookie中的内容" + value;
}
}
也可以直接从Request的Header中获取:
@RestController
@RequestMapping("/cookie")
public class CookieReadAWriteController {
@GetMapping("/read/annotation")
/**
* @param value
* @return
*/
@GetMapping("/read/entity")
public String cookieReadEntity(RequestEntity<String> entity) {
HttpHeaders headers = entity.getHeaders();
List<String> cookie = headers.get("Cookie");
return "当前Cookie中的内容" + cookie;
}
}
使用Annotatin是直接标记Cookie的key来获取value。而使用RequestEntity需要从头中先获取Cookie的内容,然后再解析key和value,存在一个key对应多个value的情况需要使用RequestEntity。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。