Springboot-10请求处理(Servlet、复杂参数、自定义)
作者:互联网
1、Servlet API:
WebRequest、ServletRequest、MultipartRequest、 HttpSession、javax.servlet.http.PushBuilder、Principal、InputStream、Reader、HttpMethod、Locale、TimeZone、ZoneId
2、复杂参数
1、Map、Model(map、model里面的数据会被放在request的请求域 ),即等价于给HttpServletRequest 放数据(request.setAttribute)。
2、Map、Model类型的参数,会返回 mavContainer.getModel();—> BindingAwareModelMap 是Model 也是Map。
/**
*请求tset,然后forward转发给success
**/
@GetMapping("/test")
public String testParams(Map<Object,String> map,
Model model,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse){
map.put("yan1","1");
model.addAttribute("yan2","2");
httpServletRequest.setAttribute("yan3","3");
httpServletResponse.addCookie(new Cookie("yan4","pige"));
return "forward:/success";
}
@GetMapping("/success")
public Map test(@RequestAttribute(value = "msg",required = false) String msg,
HttpServletRequest httpRequest){
Map<String,Object> objectStringMap = new HashMap<>();
Object map = httpRequest.getAttribute("yan1");
Object model = httpRequest.getAttribute("yan2");
Object request = httpRequest.getAttribute("yan3");
Object response = httpRequest.getAttribute("yan4");
objectStringMap.put("yan1",map);
objectStringMap.put("yan2",model);
objectStringMap.put("yan3",request);
objectStringMap.put("yan4",response);
return objectStringMap;
}
3、自定义参数
前端请求提交的参数,后端默认接受为一个对象;
访问:http://localhost:8003/user/user?name=%22zhangsan%22
@GetMapping("/user")
public User user(User user){
return user;
}
结果:
{"id":null,"name":"\"zhangsan\"","password":null,"sex":null,"age":null,"createTime":null,"updateTime":null,"deleted":null,"version":null,"departId":null}
标签:httpRequest,10,Springboot,自定义,Map,objectStringMap,user,put,null 来源: https://blog.csdn.net/weixin_45176509/article/details/120753532