本文共 2322 字,大约阅读时间需要 7 分钟。
package com.adapter.mcs.server.controller;import org.springframework.http.ResponseCookie;import org.springframework.http.server.reactive.ServerHttpRequest;import org.springframework.web.bind.annotation.*;import org.springframework.web.server.ServerWebExchange;import reactor.core.publisher.Mono;@RestControllerpublic class TestController { @GetMapping(path = "/testServletHttpRequest") public MonotestServletHttpRequest(ServerHttpRequest serverHttpRequest, ServerWebExchange exchange) { /**收到cookie参数*/ System.out.println("====收到的Cookies: " + serverHttpRequest.getCookies()); /**调用serveice进行处理*/ /**修改cookies并返回*/ ResponseCookie responseCookie = ResponseCookie.from("userId", "modify_value1").build(); exchange.getResponse().addCookie(responseCookie); return Mono.just("response success: "); } @GetMapping(path = "/testRequestParam/{pathValue}") public Mono testRequestParam(@PathVariable(name = "pathValue") String pathValue, @RequestHeader("header_key1") String header_key1, @CookieValue("userId") String userId, ServerWebExchange exchange) { /**收到param参数*/ System.out.println("====收到的pathValue: " + pathValue + " header_key1: " + header_key1 +" userId: " + userId); /**调用serveice进行处理*/ /**修改cookies并返回*/ ResponseCookie responseCookie = ResponseCookie.from("userId", "modify_value2").build(); exchange.getResponse().addCookie(responseCookie); return Mono.just("response success: "); } @PostMapping(path = "/testRequestBody") public Mono testRequestBody(String testBody, ServerHttpRequest serverHttpRequest, ServerWebExchange exchange) { /**收到参数*/ System.out.println("====收到的testBody: " + testBody + " serverHttpRequest: " + serverHttpRequest.getHeaders() +" cookies: " + serverHttpRequest.getCookies()); /**调用serveice进行处理*/ /**修改cookies并返回*/ ResponseCookie responseCookie = ResponseCookie.from("userId", "modify_value3").build(); exchange.getResponse().addCookie(responseCookie); return Mono.just("response success: "); }}
转载地址:http://oylcn.baihongyu.com/