经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
Spring Boot详解各类请求和响应的处理方法
来源:jb51  时间:2022/7/19 9:55:39  对本文有异议

1. HttpServletRequest与HttpServletResponse

浏览器输入:http://localhost:8080/community/alpha/http?code=10

  1. @RequestMapping("/http")
  2. public void http(HttpServletRequest request, HttpServletResponse response) {
  3. // 获取请求数据
  4. System.out.println(request.getMethod());
  5. System.out.println(request.getServletPath());
  6. Enumeration<String> enumeration = request.getHeaderNames();
  7. while (enumeration.hasMoreElements()) {
  8. String name = enumeration.nextElement();
  9. String value = request.getHeader(name);
  10. System.out.println(name + ": " + value);
  11. }
  12. System.out.println(request.getParameter("code"));
  13. // 返回响应数据
  14. response.setContentType("text/html;charset=utf-8");
  15. try {
  16. PrintWriter writer = response.getWriter();
  17. writer.write("<h1>nowcoder</h1>");
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. }

前端页面显示:nowcoder

同时,IDEA控制台输出:

GET
/alpha/http
host: localhost:8080
connection: keep-alive
sec-ch-ua: " Not;A Brand";v="99", "Google Chrome";v="97", "Chromium";v="97"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
sec-fetch-site: none
sec-fetch-mode: navigate
sec-fetch-user: ?1
sec-fetch-dest: document
accept-encoding: gzip, deflate, br
accept-language: zh-CN,zh;q=0.9
cookie: Idea-10659edd=72fa12c3-9b68-4da6-8b68-38a81c822aa0
10

2. GET类型的请求

AlphaController中增加方法,处理GET类型的请求。

2.1 /students?current=1&limit=20

浏览器输入:http://localhost:8080/community/alpha/students?current=1&limit=100

  1. @GetMapping("/students")
  2. @ResponseBody
  3. public String getStudents(
  4. @RequestParam(name = "current" ,required = false, defaultValue = "1") int current,
  5. @RequestParam(name = "limit" ,required = false, defaultValue = "10") int limit) {
  6. System.out.println(current);
  7. System.out.println(limit);
  8. return "some students";
  9. }

前端页面显示:some students

同时,IDEA控制台输出:

1
100

2.2 /student/123

浏览器输入:http://localhost:8080/community/alpha/student/123

  1. @GetMapping("/student/{id}")
  2. @ResponseBody
  3. public String getStudent(@PathVariable("id") int id) {
  4. System.out.println(id);
  5. return "one student";
  6. }

前端页面显示:one students

同时,IDEA控制台输出:

123

3. POST类型的请求

AlphaController中增加方法,处理POST类型的请求。

浏览器输入:http://localhost:8080/community/html/student.html

  1. @PostMapping("/student")
  2. @ResponseBody
  3. public String saveStudent(String name, int age) {
  4. System.out.println(name);
  5. System.out.println(age);
  6. return "success";
  7. }

resources/static/html/student.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>增加学生</title>
  6. </head>
  7. <body>
  8. <form method="post" action="/community/alpha/student">
  9. <p>
  10. 姓名:<input type="text" name="name">
  11. </p>
  12. <p>
  13. 年龄:<input type="text" name="age">
  14. </p>
  15. <p>
  16. <input type="submit" value="保存">
  17. </p>
  18. </form>
  19. </body>
  20. </html>

前端页面显示一个表单,包含两行:姓名、年龄,还有一个 “保存” 按钮。

输入 “Lebron”、38 后,点击 “保存” 后,显示 “success”。

4. 响应HTML格式的数据

AlphaController中增加方法,向浏览器响应HTML格式的数据。

4.1 使用ModelAndView

浏览器输入:http://localhost:8080/community/alpha/teacher

  1. @GetMapping("/teacher")
  2. public ModelAndView getTeacher() {
  3. ModelAndView mav = new ModelAndView();
  4. mav.addObject("name", "Lebron");
  5. mav.addObject("age", 38);
  6. mav.setViewName("/demo/view");
  7. return mav;
  8. }

前端页面显示:

Lebron
38

4.2 使用Model

浏览器输入:http://localhost:8080/community/alpha/school

  1. @GetMapping("/school")
  2. public String getSchool(Model model) {
  3. model.addAttribute("name", "xx大学");
  4. model.addAttribute("age", 100);
  5. return "/demo/view";
  6. }

resources/templates/demo/view.html

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Teacher</title>
  6. </head>
  7. <body>
  8. <p th:text="${name}"></p>
  9. <p th:text="${age}"></p>
  10. </body>
  11. </html>

前端页面显示:

xx大学
100

5. 响应JSON格式的数据

AlphaController中增加方法,向浏览器响应JSON格式的数据。

5.1 单组数据

浏览器输入:http://localhost:8080/community/alpha/emp

  1. @GetMapping("/emp")
  2. @ResponseBody
  3. public Map<String, Object> getEmp() {
  4. Map<String, Object> emp = new HashMap<>();
  5. emp.put("name", "Kitty");
  6. emp.put("age", 20);
  7. emp.put("salary", 12000.00);
  8. return emp;
  9. }

前端页面显示:

{"name":"Kitty","salary":12000.0,"age":20}

5.2 多组数据

浏览器输入:http://localhost:8080/community/alpha/emps

  1. @GetMapping("/emps")
  2. @ResponseBody
  3. public List<Map<String, Object>> getEmps() {
  4. List<Map<String, Object>> list = new ArrayList<>();
  5. Map<String, Object> emp = new HashMap<>();
  6. emp.put("name", "Tom");
  7. emp.put("age", 20);
  8. emp.put("salary", 12000.00);
  9. list.add(emp);
  10. emp = new HashMap<>();
  11. emp.put("name", "Jerry");
  12. emp.put("age", 18);
  13. emp.put("salary", 15000.00);
  14. list.add(emp);
  15. emp = new HashMap<>();
  16. emp.put("name", "Leo");
  17. emp.put("age", 25);
  18. emp.put("salary", 8000.00);
  19. list.add(emp);
  20. return list;
  21. }

前端页面显示:

[{"name":"Tom","salary":12000.0,"age":20},{"name":"Jerry","salary":15000.0,"age":18},{"name":"Leo","salary":8000.0,"age":25}]

到此这篇关于Spring Boot详解各类请求和响应的处理方法的文章就介绍到这了,更多相关Spring Boot请求和响应的处理内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号