代码取自一位好友,好友写的demo
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;
@RestController
public class TestController {
@GetMapping(path = "/testServletHttpRequest")
public Mono<String> testServletHttpRequest(ServerHttpRequest serverHttpRequest, ServerWebExchange exchange) {
System.out.println("====收到的Cookies: " + serverHttpRequest.getCookies());
ResponseCookie responseCookie = ResponseCookie.from("userId", "modify_value1").build();
exchange.getResponse().addCookie(responseCookie);
return Mono.just("response success: ");
}
@GetMapping(path = "/testRequestParam/{pathValue}")
public Mono<String> testRequestParam(@PathVariable(name = "pathValue") String pathValue,
@RequestHeader("header_key1") String header_key1,
@CookieValue("userId") String userId,
ServerWebExchange exchange) {
System.out.println("====收到的pathValue: " + pathValue + " header_key1: " + header_key1 +" userId: " + userId);
ResponseCookie responseCookie = ResponseCookie.from("userId", "modify_value2").build();
exchange.getResponse().addCookie(responseCookie);
return Mono.just("response success: ");
}
@PostMapping(path = "/testRequestBody")
public Mono<String> testRequestBody(String testBody, ServerHttpRequest serverHttpRequest,
ServerWebExchange exchange) {
System.out.println("====收到的testBody: " + testBody + " serverHttpRequest: " + serverHttpRequest.getHeaders() +" cookies: " + serverHttpRequest.getCookies());
ResponseCookie responseCookie = ResponseCookie.from("userId", "modify_value3").build();
exchange.getResponse().addCookie(responseCookie);
return Mono.just("response success: ");
}
}