SpringBoot 获取 Get 请求参数详解
叙述利用Spring Boot来制作Web应用,就必定会涉及到前端与后台之间互相传递参数。下面演示Controller如何接收以GET方式传递过来的参数。解决方案参数直接在路径中假设请求地址是如下这种RESTful风格,hangge这个参数值直接放在路径里面:http://localhost:8080/hello/hanggeimport org.springframework.web.bind.
叙述
利用 Spring Boot 来制作 Web 应用,就必定会涉及到前端与后台之间互相传递参数。
下面演示 Controller 如何接收以 GET 方式传递过来的参数。
解决方案
参数直接在路径中
假设请求地址是如下这种 RESTful 风格,hangge 这个参数值直接放在路径里面:
http://localhost:8080/hello/hangge
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
@RestController
public class HelloController {
@GetMapping("/hello/{name}")
public String hello(@PathVariable("name") String name) {
return "获取到的name是:" + name;
}
}
参数跟在 ? 号后面
假设请求地址是如下这种传统方式,参数跟在问号后面:
http://localhost:8080/hello?name=hangge
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("/hello")
public String hello(@RequestParam("name") String name) {
return "获取到的name是:" + name;
}
}
如果没有传递参数 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("/hello")
public String hello(@RequestParam(name = "name", required = false) String name) {
return "获取到的name是:" + name;
}
}
或者可以指定个默认值,当没有传递参数时自动使用默认值:
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("/hello")
public String hello(@RequestParam(name = "name", defaultValue = "xxx") String name) {
return "获取到的name是:" + name;
}
}
使用 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("/hello")
public String hello(@RequestParam Map<String, Object> params) {
return "name:" + params.get("name") + "<br>age:" + params.get("age");
}
}
接收一个数组
假设请求地址是如下这种,有多个同名参数:
http://localhost:8080/hello?name=hangge&name=google
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("/hello")
public String hello(@RequestParam("name") String[] names) {
String result = "";
for(String name:names){
result += name + "<br>";
}
return result;
}
}
使用对象来接收参数
如果一个 get 请求的参数太多,我们构造一个对象来简化参数的接收方式:
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(User user) {
return "name:" + user.getName() + "<br> age:" + user.getAge();
}
}
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;
}
}
如果传递的参数有前缀,且前缀与接收实体类的名称相同,那么参数也是可以正常传递的:
指定参数前缀
如果传递的参数有前缀,且前缀与接收实体类的名称不同相,那么参数无法正常传递:
我们可以结合 @InitBinder 解决这个问题,通过参数预处理来指定使用的前缀为 u
除了在 Controller 里单独定义预处理方法外,我们还可以通过 @ControllerAdvice 结合 @InitBinder 来定义全局的参数预处理方法,方便各个 Controller 使用。具体做法参考我之前的文章:
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(@ModelAttribute("u") User user) {
return "name:" + user.getName() + "<br> age:" + user.getAge();
}
@InitBinder("u")
private void initBinder(WebDataBinder binder) {
binder.setFieldDefaultPrefix("u.");
}
}
构造多个对象来接收参数
如果一个 get 请求的参数分属不同的对象,也可以使用多个对象来接收参数:
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(User user, Phone phone) {
return "name:" + user.getName() + "<br> age:" + user.getAge()
+ "<br> number:" + phone.getNumber();
}
}
public class Phone {
private String number;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
更多推荐
所有评论(0)