详 解 @pathvariable 、 @requestheader 、 @modelattribute 、 @requestparam 、 @matrixvariable、@cookievalue、@requestbody
2.接收参数相关注解应用实例1.需求: 演示各种方式提交数据/参数给服务器,服务器如何使用注解接收
2.应用实例演示
需求: 演示各种方式提交数据/参数给服务器,服务器如何使用注解接收
创建src\main\resources\static\index.html
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>index</title></head><body><h2>hello, llp</h2>基本注解:<hr/><a href="/monster/200/jack" rel="external nofollow" >@pathvariable-路径变量 monster/200/jack</a><br/><br/></body></html>
@pathvariable 使用演示@pathvariable 使用,创建src\main\java\com\llp\springboot\controller\parametercontroller.java, 完成测试
@restcontrollerpublic class parametercontroller { /** * /monster/{id}/{name} 解读 * 1. /monster/{id}/{name} 构成完整请求路径 * 2. {id} {name} 就是占位变量 * 3. @pathvariable("name"): 这里name 和{name} 命名保持一致 * 4. string name_ 这里自定义,和{name}命名无关 * 5. @pathvariable map<string, string> map 把所有传递的值传入map * 6. 可以看下@pathvariable源码 */ @getmapping("/monster/{id}/{name}") public string pathvariable(@pathvariable("id") integer id, @pathvariable("name") string name, @pathvariable map<string, string> map) { system.out.println("id-" + id); system.out.println("name-" + name); system.out.println("map-" + map); return "success"; }}
@requestheader 使用演示@requestheader 使用,修改 parametercontroller.java , 完成测试
√ 修改 index.html
<a href="/requestheader" rel="external nofollow" >@requestheader-获取http请求头 </a><br/><br/>
√ 修改 parametercontroller.java
/** * @requestheader("host") 获取http请求头的 host信息 * @requestheader map<string, string> header: 获取到http请求的所有信息 */@getmapping("/requestheader")public string requestheader(@requestheader("host") string host, @requestheader map<string, string> header, @requestheader("accept") string accept) { system.out.println("host-" + host); system.out.println("header-" + header); system.out.println("accept-" + accept); return "success";}
@requestparam 使用演示@requestparam 使用,修改 parametercontroller.java , 完成测试
√ 修改 index.html
<a href="/hi?name=wukong&fruit=apple&fruit=pear&id=300&address=北京" rel="external nofollow" >@requestparam-获取请求参数</a><br/><br/>
√ 修改 parametercontroller.java
/** * @param username wukong * @param fruits list<string> fruits 接收集合 [apple, pear] * @param paras map<string, string> paras 如果我们希望将所有的请求参数的值都获取到, * 可以通过@requestparam map<string, string> paras这种方式 * 一次性的接收所有的请求参数 {name=wukong, fruit=apple, id=300, address=北京} * 如果接收的某个参数中有多个之值比如这里fruits是一个集合,从map中只能拿到一个 * 可以理解map底层会将相同的key的value值进行覆盖 * @return * @requestparam */@getmapping("/hi")public string hi(@requestparam(value = "name") string username, @requestparam("fruit") list<string> fruits, @requestparam map<string, string> paras) { //username-wukong system.out.println("username-" + username); //fruit-[apple, pear] system.out.println("fruit-" + fruits); //paras-{name=wukong, fruit=apple, id=300, address=北京} system.out.println("paras-" + paras); return "success";}
@cookievalue 使用演示@cookievalue 使用,修改 parametercontroller.java , 完成测试
√ 修改 index.html
<a href="/cookie" rel="external nofollow" >@cookievalue-获取cookie值</a><br/><br/>
√ 修改 parametercontroller.java
/** * 因为我的浏览器目前没有cookie,我们可以自己设置cookie[技巧还是非常有用] * 如果要测试,可以先写一个方法,在浏览器创建对应的cookie * 说明 1. value = "cookie_key" 表示接收名字为 cookie_key的cookie * 2. 如果浏览器携带来对应的cookie , 那么 后面的参数是string ,则接收到的是对应对value * 3. 后面的参数是cookie ,则接收到的是封装好的对应的cookie */@getmapping("/cookie")public string cookie(@cookievalue(value = "cookie_key", required = false) string cookie_value, httpservletrequest request, @cookievalue(value = "username", required = false) cookie cookie) { system.out.println("cookie_value-" + cookie_value); if (cookie != null) { system.out.println("username-" + cookie.getname() + "-" + cookie.getvalue()); } system.out.println("-------------------------"); cookie[] cookies = request.getcookies(); for (cookie cookie1 : cookies) { system.out.println(cookie1.getname() + "=>" + cookie1.getvalue()); } return "success";}
@requestbody 使用演示@requestbody 使用,修改 parametercontroller.java , 完成测试
√ 修改 index.html
<hr/><h2>测试@requestbody获取数据: 获取post请求体</h2><form action="/save" method="post"> 姓名: <input name="name"/> <br> 年龄: <input name="age"/> <br/> <input type="submit" value="提交"/></form>
√ 修改 parametercontroller.java
/** * @requestbody 是整体取出post请求内容 */@postmapping("/save")public string postmethod(@requestbody string content) { system.out.println("content-" + content); return "success";}
@requestattribute 和 @sessionattribute使用
演示@requestattribute @sessionattribute使用,创建 com/hspedu/web/controller/requestcontroller.java , 完成测试
√ 修改 index.html
<a href="/login" rel="external nofollow" >@requestattribute、@sessionattribute-获取request域、session属性-</a>
√ 创建 requestcontroller.java
@getmapping("/login") public string login(httpservletrequest request) { request.setattribute("user", "llp"); //向session中添加数据 request.getsession().setattribute("website", "http://www.baidu.com"); //这里需要使用forward关键字,如果不适用则会走视图解析器,这 //里视图解析器前缀配置的是/ 后缀配置的.html ---> /ok.html //而请求转发在服务器端执行,/被解析成 ip:port/工程路径 //进而最终得到的完整路径是 ip:port/工程路径/ok.html //但是我们这里希望访问的是 ip:port/工程路径/ok这个请求路径 //因此这里手动的设置forward:/ok ,底层会根据我们设置的路径进行请求转发 return "forward:/ok"; } @getmapping("ok") //返回字符串,不走视图解析器 @responsebody public string ok(@requestattribute(value = "user", required = false) string username, @sessionattribute(value = "website",required = false) string website, httpservletrequest request) { system.out.println("username= " + username); system.out.println("通过servlet api 获取 username-" + request.getattribute("user")); system.out.println("website = " + website); system.out.println("通过servlet api 获取 website-"+request.getsession().getattribute("website")); return "success"; }}
3.复杂参数1.基本介绍在开发中,springboot 在响应客户端请求时,也支持复杂参数
map、model、errors/bindingresult、redirectattributes、servletresponse、sessionstatus、 uricomponentsbuilder、servleturicomponentsbuilder、httpsession
map、model 数据会被放在 request 域, 底层 request.setattribute()
redirectattributes 重定向携带数据
2.复杂参数应用实例####1.说明 : 演示复杂参数的使用,重点: map、model、servletresponse
2.代码实现
//响应一个注册请求@getmapping("/register")public string register(map<string,object> map, model model, httpservletresponse response) { //如果一个注册请求,会将注册数据封装到map或者model //map中的数据和model的数据,会被放入到request域中 map.put("user","llp"); map.put("job","码农"); model.addattribute("sal", 2500); //一会我们再测试response使用 //我们演示创建cookie,并通过response 添加到浏览器/客户端 cookie cookie = new cookie("email", "123@sohu.com"); response.addcookie(cookie); //请求转发 return "forward:/registerok";}@responsebody@getmapping("/registerok")public string registerok(httpservletrequest request) { system.out.println("user-" + request.getattribute("user")); system.out.println("job-" + request.getattribute("job")); system.out.println("sal-" + request.getattribute("sal")); return "success";}
4.自定义对象参数-自动封装1.基本介绍在开发中,springboot 在响应客户端请求时,也支持自定义对象参数
完成自动类型转换与格式化
支持级联封装
2.自定义对象参数-应用实例1.需求说明 : 演示自定义对象参数使用,完成自动封装,类型转换
2.代码实现
1.创建src\main\resources\static\save.html
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>添加妖怪</title></head><body><h2>添加妖怪-坐骑[测试封装 pojo;]</h2><form action="/savemonster" method="post"> 编号: <input name="id" value="100"><br/> 姓名: <input name="name" value="牛魔王"/><br/> 年龄: <input name="age" value="120"/> <br/> 婚否: <input name="ismarried" value="true"/> <br/> 生日: <input name="birth" value="2000/11/11"/> <br/> <!--注意这里car对象是monster的属性,给对象属性赋值时需要以对象名.字段名的方式--> 坐骑:<input name="car.name" value="法拉利"/><br/> 价格:<input name="car.price" value="99999.9"/> <input type="submit" value="保存"/></form></body></html>
2.修改src\main\java\com\llp\springboot\controller\parametercontroller.java
@postmapping("/savemonster")public string savemonster(monster monster) { system.out.println("monster= " + monster); return "success";}
以上就是springboot接收参数使用的注解是什么的详细内容。
