SpringBoot接收前端参数的三种方法
首先第一种代码:
访问路径:http://localhost:8080/hello/5
@RestController
public class ControllerTest {
//访问路径:http://localhost:8080/hello/5
@GetMapping(value = "/hello/{id}")
public String hello(@PathVariable("id") Integer id){
return "ID:" + id;
}
}
第二种:
可以通过注解@RequestParam(“xxx”)来绑定指定的参数,在这里声明的参数为ids,但是@RequestParam()注解中确实id,这个时候前端传的值就应该为id,如果前台传送的是ids的话,在这里是接收不到的。
访问路径:http://localhost:8080/hello?id=1551
@RestController
public class ControllerTest {
//访问路径:http://localhost:8080/hello?id=1551
@GetMapping(value = "/hello")
public String hello(@RequestParam("id") Integer ids){
return "ID:" + id;
}
}
第三种像这种,如果传进来的参数为空的话,可以设置为默认的:
//访问路径:http://localhost:8080/hello?id=1551
@GetMapping(value = "/hello")
public String hello(@RequestParam(value = "id", required = false, defaultValue = "5") Integer id){
return "ID:" + id;
}
第四种,接收map参数
@RequestBydy 只能是对象 , entity, Map , List
(1) 传实体对象
(2) 传 map
(3) 传集合
body form-data传参
后端接收