测试开发进阶——spring boot——MVC——HttpServletRespon设置返回header、返回cookie、返回体
控制器:
package com.awaimai.web; import org.springframework.web.bind.annotation.*; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @RestController public class kzq { @RequestMapping(value = "/test2") public String test2(HttpServletResponse response) { response.setHeader("content-type", "text/html;charset=UTF-8"); //告知浏览器使用什么码表 //告知客户端不缓存 response.setHeader("pragma", "no-cache"); response.setHeader("cache-control", "no-cache"); response.setDateHeader("expires", 0); //添加Cookie response.addCookie(new Cookie("cook-001-name" ,"cookie-001-value")); response.addCookie(new Cookie("cook-002-name" ,"cookie-002-value")); response.setStatus(404); //我们将状态设置成404 return "success"; } }
post请求: