一、参数直接在路径中
1.假设请求地址是如下这种 restful 风格
hangge 这个参数值直接放在路径里面:
http://localhost:8080/helloworld/张三
import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.pathvariable; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class helloworldcontroller { @getmapping("/helloworld/{name}") public string helloworld(@pathvariable("name") string name) { return "获取到的name是:" + name; } }
二、参数跟在 ? 号后面
1.获取参数的基本方法
(1)假设请求地址是如下这种传统方式,参数跟在问号后面:
http://localhost:8080/helloworld1?name=张三
(2)controller 可以这么获取该参数:
import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.bind.annotation.restcontroller; import org.springframework.web.bind.annotation.getmapping; @restcontroller public class hellocontroller { @getmapping("/helloworld1") public string helloworld1(@requestparam("name") string name) { return "获取到的name是:" + name; } }
2.参数没有传递的情况
(1)如果没有传递参数 controller 将会报错,我们可以使用 required = false 标注参数是非必须的。
import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.bind.annotation.restcontroller; import org.springframework.web.bind.annotation.getmapping; @restcontroller public class hellocontroller { @getmapping("/helloworld2") public string helloworld2(@requestparam(name = "name", required = false) string name) { return "获取到的name是:" + name; } }
(2)或者可以指定个默认值,当没有传递参数时自动使用默认值:
import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.bind.annotation.restcontroller; import org.springframework.web.bind.annotation.getmapping; @restcontroller public class hellocontroller { @getmapping("/helloworld3") public string helloworld3(@requestparam(name = "name", defaultvalue = "xxx") string name) { return "获取到的name是:" + name; } }
3.使用 map 来接收参数
(1)controller 还可以直接使用 map 来接收所有的请求参数:
import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.bind.annotation.restcontroller; import org.springframework.web.bind.annotation.getmapping; import java.util.map; @restcontroller public class hellocontroller { @getmapping("/helloworld4") public string helloworld4(@requestparam map<string, object> params) { return "name:" + params.get("name") + "<br>age:" + params.get("age"); } }
(2)下面是一个简单的测试样例:
4.接收一个数组
(1)假设请求地址是如下这种,有多个同名参数:
http://localhost:8080/helloworld5?name=zhangsan&name=lisi
(2)我们可以定义一个数组类型的参数来接收:
import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.bind.annotation.restcontroller; import org.springframework.web.bind.annotation.getmapping; @restcontroller public class hellocontroller { @getmapping("/helloworld5") public string helloworld5(@requestparam("name") string[] names) { string result = ""; for(string name:names){ result += name + "<br>"; } return result; } }
附:使用对象来接收参数
1.基本用法
(1)如果一个 get 请求的参数太多,我们构造一个对象来简化参数的接收方式:
import org.springframework.web.bind.annotation.restcontroller; import org.springframework.web.bind.annotation.getmapping; @restcontroller public class hellocontroller { @getmapping("/helloworld6") public string helloworld6(user user) { return "name:" + user.getname() + "<br> age:" + user.getage(); } }
(2)user 类的定义如下,到时可以直接将多个参数通过 getter、setter 方法注入到对象中去:
public class user { private string name; private integer age; public string getname() { return name; } public void setname(string name) { this.name = name; } public integer getage() { return age; } public void setage(integer age) { this.age = age; } }
(3)下面是一个简单的测试样例:
(4)如果传递的参数有前缀,且前缀与接收实体类的名称相同,那么参数也是可以正常传递的:
2.指定参数前缀
(1)如果传递的参数有前缀,且前缀与接收实体类的名称不同相,那么参数无法正常传递:
(2)我们可以结合 @initbinder 解决这个问题,通过参数预处理来指定使用的前缀为 u.
除了在 controller 里单独定义预处理方法外,我们还可以通过 @controlleradvice 结合 @initbinder 来定义全局的参数预处理方法,方便各个 controller 使用。具体做法参考我之前的文章:
- springboot - @controlleradvice的使用详解3(请求参数预处理 @initbinder)
import org.springframework.web.bind.webdatabinder; import org.springframework.web.bind.annotation.*; @restcontroller public class hellocontroller { @getmapping("/helloworld7") public string helloworld7(@modelattribute("u") user user) { return "name:" + user.getname() + "<br> age:" + user.getage(); } @initbinder("u") private void initbinder(webdatabinder binder) { binder.setfielddefaultprefix("u."); } }
(3)重启程序可以看到参数以及成功接收了:
3.构造多个对象来接收参数
(1)如果一个 get 请求的参数分属不同的对象,也可以使用多个对象来接收参数:
import org.springframework.web.bind.annotation.restcontroller; import org.springframework.web.bind.annotation.getmapping; @restcontroller public class hellocontroller { @getmapping("/helloworld8") public string helloworld8(user user, phone phone) { return "name:" + user.getname() + "<br> age:" + user.getage() + "<br> number:" + phone.getnumber(); } }
(2)新增的 phone 类定义如下:
public class phone { private string number; public string getnumber() { return number; } public void setnumber(string number) { this.number = number; } }
(3)下面是一个简单的测试样例:
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论