三.RequestMapping
由上述案例可得知,我们在演示时用的都是RequestMapping,但是在实际开发中RequestMapping并不常用,而且安全系数低,在我们向浏览器发送请求时,如果该方法体用的RequestMapping注解,我们可以使用任意的请求方式进行访问,当遇到需要添加数据的情况时,一些不法分子发现这些漏洞时会使用循环get请求不断向服务器发送请求添加数据,造成服务器压力崩溃,所以在实际开发中我们尽量使用标明好的请求方式注解,如:GetMapping、POSTMapping等等....
RequestMapping=GetMapping+POSTMapping+PutMapping+DeleteMapping
四.返回值
1.返回JSON(1)
@RequestMapping("/hello1") public void hello1(HttpServletResponse response){ Map<Object, Object> map = new HashMap<>(); map.put("code",200); map.put("msg","成功添加"); try { ResponseUtil.writeJson(response,map); } catch (Exception e) { e.printStackTrace(); } }
运行结果:
2. Map 返回JSON(2)
@ResponseBody @RequestMapping("/hello2") public Map hello2(HttpServletResponse response){ Map<Object, Object> map = new HashMap<>(); map.put("code",200); map.put("msg","成功添加"); return map; }
运行结果:
3.String
@RequestMapping("/hello3") public String hello3(){ return "index"; }
运行结果:
4. String+model
@RequestMapping("/hello4") public String hello4(Model model, HttpServletRequest request){ model.addAttribute("currentName","死仔"); request.setAttribute("location","网左网右"); return "index"; }
运行结果:
5.ModelAndView
@RequestMapping("/hello5") public ModelAndView hello5(){ ModelAndView mv = new ModelAndView(); mv.addObject("sign","真的呆"); mv.setViewName("index"); return mv; }
运行结果: