今日总结
今天主要学习springmvc;今天首先学习了如何获取请求头和cookie,以及一些注解相关的原理
如下图所示,我们获取到了网页的请求头的host
示例
package com.aurora.header; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @ResponseBody @RequestMapping("header") public class HeaderController { @GetMapping("data") public String data(@RequestHeader("Host") String host){ System.out.println("host = " + host); return "host = " + host; } }
package com.aurora.Cookie; import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("cookie") @ResponseBody public class CookieController { @RequestMapping("data") public String data(@CookieValue(value = "cookieName") String value){ System.out.println("value = " + value); return value; } @GetMapping("save") public String save(HttpServletResponse response){ Cookie cookie = new Cookie("cookieName","root"); response.addCookie(cookie); return "ok"; } }
作者:冰稀饭Aurora
出处:https://www.cnblogs.com/rsy-bxf150/p/17793000.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端