提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 一、@RequestBody补充
- 二、@PathVariable
- 三.@RequestPart:
- 四.@ResponseBody
- 五.@CookieValue,@SessionAttribute,@RequestHeader
前言
提示:这里可以添加本文要记录的大概内容:
接着上一篇的mvc(上),下面继续接着写,上篇回顾,讲到了
a. @RequestMapping: 路由映射
b. @RequestParam: 后端参数重命名
c. @RequestBody: 接收JSON类型的参数
这三个类注解,接下来开启新篇
提示:以下是本篇文章正文内容,下面案例可供参考
一、@RequestBody补充
public class UserInfo {private Integer id;private String username;private Integer age;@Overridepublic String toString() {return "UserInfo{" +"id=" + id +", username='" + username + '\'' +", age=" + age +'}';}public Integer getId() {return id;}public String getUsername() {return username;}public Integer getAge() {return age;}public void setId(Integer id) {this.id = id;}public void setUsername(String username) {this.username = username;}public void setAge(Integer age) {this.age = age;}
}
java">@RequestMapping("/r6")public String r6(@RequestBody UserInfo userInfo){return userInfo.toString();}
这里就是再补充一下中Java对象转json的写法
java">public class JSONUtils {public static void main(String[] args) throws JsonProcessingException {ObjectMapper objectMapper = new ObjectMapper();UserInfo userInfo = new UserInfo();userInfo.setUsername("zhangsan");userInfo.setAge(18);userInfo.setId(10);//对象转jsonString s = objectMapper.writeValueAsString(userInfo);System.out.println(s);UserInfo userInfo1 = objectMapper.readValue(s,UserInfo.class);System.out.println(userInfo1);}
}
第一行就是转成的json格式
然后把这个复制到postman中
java">{"id":10,"username":"zhangsan","age":18}
requestbody是body并且是json格式
然后就可以了
二、@PathVariable
这个就是修改url的路径
我演示一下
java"> @RequestMapping("/r7/{name}/{age}")public String r7(@PathVariable String name,@PathVariable Integer age){return "name:"+name+"age:"+age;}
三.@RequestPart:
java">@RequestMapping("/r9")public String r9(@RequestPart MultipartFile file) throws IOException {String filename = file.getOriginalFilename();file.transferTo(new File("D:/images/"+filename));return filename;}
这个这样子写别忘了
四.@ResponseBody
这个二记住一下@RestController = @Controller + @ResponseBody
五.@CookieValue,@SessionAttribute,@RequestHeader
@CookieValue: 从Cookie中获取值
@SessionAttribute: 从Session中获取值
@RequestHeader: 从Header中获取值
java">public class RequestController {@RequestMapping("/getCookie")public String getCookie(HttpServletRequest request){Cookie[] cookies = request.getCookies();
// Arrays.stream(cookies).forEach(x -> System.out.println(x.getName()+":"+x.getValue()));if (cookies!=null){for (Cookie c:cookies) {System.out.println(c.getName()+":"+c.getValue());}return "获取cookie成功";}return "获取失败";}@RequestMapping("/getCookie2")public String getCookie2(@CookieValue("bite")String bite){return "bite"+bite;}@RequestMapping("/setSession")public String setSession(HttpServletRequest request){HttpSession session = request.getSession();session.setAttribute("userName","wangwu");return "设置session成功";}@RequestMapping("/getSession")public String getSession(HttpServletRequest request){HttpSession session = request.getSession();String username = (String) session.getAttribute("username");return "登录用户"+username;}@RequestMapping("/getSession2")public String getSession(HttpSession session){String username = (String) session.getAttribute("userName");return "登录用户:"+username;}@RequestMapping("/getSession3")public String getSession(@SessionAttribute("userName")String username){return "登录用户:"+username;}@RequestMapping("/getHeader")public String getHeader(HttpServletRequest request){String userAgent = request.getHeader("User-Agent");return "userAgent:"+userAgent;}
}
个人感觉还是HttpServletRequest 更加通用,当然了setsession还是得session.setAttribute