1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| @Controller @RequestMapping("/alpha") public class AlphaController {
@Autowired private AlphaService alphaService;
@RequestMapping("/http") public void http(HttpServletRequest request, HttpServletResponse response) { System.out.println(request.getMethod()); System.out.println(request.getServletPath()); Enumeration<String> enumeration = request.getHeaderNames(); while (enumeration.hasMoreElements()) { String name = enumeration.nextElement(); String value = request.getHeader(name); System.out.println(name + ":" + value); } System.out.println(request.getParameter("code"));
response.setContentType("text/html;charset=utf-8"); try ( PrintWriter writer = response.getWriter(); ) { writer.write("<h1>牛客网</h1>"); } catch (IOException e) { e.printStackTrace(); } }
@RequestMapping(path = "/students", method = RequestMethod.GET) @ResponseBody public String getStudents(@RequestParam(name = "current", required = false, defaultValue = "1") int current, @RequestParam(name = "limit", required = false, defaultValue = "10") int limit) { System.out.println(current); System.out.println(limit); return "some students"; }
@RequestMapping(path = "/student/{id}", method = RequestMethod.GET) @ResponseBody public String getStudent(@PathVariable("id") int id) { System.out.println(id); return "a student"; }
@RequestMapping(path = "/student", method = RequestMethod.POST) @ResponseBody public String saveStudent(String name, int age) { System.out.println(name); System.out.println(age); return "success"; }
@RequestMapping(path = "/teacher", method = RequestMethod.GET) public ModelAndView getTeacher() { ModelAndView mav = new ModelAndView(); mav.addObject("name", "字节跳动"); mav.addObject("age", 8); mav.setViewName("/demo/view"); return mav; }
@RequestMapping(path = "/school", method = RequestMethod.GET) public String getSchool(Model model) { model.addAttribute("name", "北京大学"); model.addAttribute("age", 100); return "/demo/view"; }
@RequestMapping(path = "/emp", method = RequestMethod.GET) @ResponseBody public Map<String, Object> getEmp() { Map<String, Object> emp = new HashMap<>(); emp.put("name", "李成风"); emp.put("age", 23); emp.put("salary", 8000.00); return emp; }
@RequestMapping(path = "/emps", method = RequestMethod.GET) @ResponseBody public List<Map<String, Object>> getEmps() { List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> emp = new HashMap<>(); emp.put("name", "李成风"); emp.put("age", 23); emp.put("salary", 8000.00); list.add(emp);
emp = new HashMap<>(); emp.put("name", "刘毅"); emp.put("age", 22); emp.put("salary", 9000.00); list.add(emp);
emp = new HashMap<>(); emp.put("name", "章七"); emp.put("age", 22); emp.put("salary", 7000.00); list.add(emp);
return list; } }
|