您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Spring Boot各类请求和响应的处理方法是什么

2025/6/19 16:18:45发布19次查看
1. httpservletrequest与httpservletresponse浏览器输入:http://localhost:8080/community/alpha/http?code=10
@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("<h2>nowcoder</h2>"); } catch (ioexception e) { e.printstacktrace(); }}
前端页面显示: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
@getmapping("/students")@responsebodypublic 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";}
前端页面显示:some students
同时,idea控制台输出:
1
100
2.2 /student/123浏览器输入:http://localhost:8080/community/alpha/student/123
@getmapping("/student/{id}")@responsebodypublic string getstudent(@pathvariable("id") int id) { system.out.println(id); return "one student";}
前端页面显示:one students
同时,idea控制台输出:
123
3. post类型的请求在alphacontroller中增加方法,处理post类型的请求。
浏览器输入:http://localhost:8080/community/html/student.html
@postmapping("/student")@responsebodypublic string savestudent(string name, int age) { system.out.println(name); system.out.println(age); return "success";}
resources/static/html/student.html
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>增加学生</title></head><body> <form method="post" action="/community/alpha/student"> <p> 姓名:<input type="text" name="name"> </p> <p> 年龄:<input type="text" name="age"> </p> <p> <input type="submit" value="保存"> </p> </form></body></html>
前端页面显示一个表单,包含两行:姓名、年龄,还有一个 “保存” 按钮。
输入 “lebron”、38 后,点击 “保存” 后,显示 “success”。
4. 响应html格式的数据在alphacontroller中增加方法,向浏览器响应html格式的数据。
4.1 使用modelandview浏览器输入:http://localhost:8080/community/alpha/teacher
@getmapping("/teacher")public modelandview getteacher() { modelandview mav = new modelandview(); mav.addobject("name", "lebron"); mav.addobject("age", 38); mav.setviewname("/demo/view"); return mav;}
前端页面显示:
lebron
38
4.2 使用model浏览器输入:http://localhost:8080/community/alpha/school
@getmapping("/school")public string getschool(model model) { model.addattribute("name", "xx大学"); model.addattribute("age", 100); return "/demo/view";}
resources/templates/demo/view.html
<!doctype html><html lang="en" xmlns:th="http://www.thymeleaf.org"><head> <meta charset="utf-8"> <title>teacher</title></head><body> <p th:text="${name}"></p> <p th:text="${age}"></p></body></html>
前端页面显示:
xx大学
100
5. 响应json格式的数据在alphacontroller中增加方法,向浏览器响应json格式的数据。
5.1 单组数据浏览器输入:http://localhost:8080/community/alpha/emp
@getmapping("/emp")@responsebodypublic map<string, object> getemp() { map<string, object> emp = new hashmap<>(); emp.put("name", "kitty"); emp.put("age", 20); emp.put("salary", 12000.00); return emp;}
前端页面显示:
{"name":"kitty","salary":12000.0,"age":20}
5.2 多组数据浏览器输入:http://localhost:8080/community/alpha/emps
@getmapping("/emps")@responsebodypublic list<map<string, object>> getemps() { list<map<string, object>> list = new arraylist<>(); map<string, object> emp = new hashmap<>(); emp.put("name", "tom"); emp.put("age", 20); emp.put("salary", 12000.00); list.add(emp); emp = new hashmap<>(); emp.put("name", "jerry"); emp.put("age", 18); emp.put("salary", 15000.00); list.add(emp); emp = new hashmap<>(); emp.put("name", "leo"); emp.put("age", 25); emp.put("salary", 8000.00); list.add(emp); return list;}
前端页面显示:
[{name:tom,salary:12000.0,age:20},{name:jerry,salary:15000.0,age:18},{name:leo,salary:8000.0,age:25}]
以上就是spring boot各类请求和响应的处理方法是什么的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product