Spring boot 如何使用get请求,接受map类型参数,数组类型参数,restful参和普通参数。
目录restful风格的参数普通类型的参数map类型的参数数组类型的参数restful风格的参数http://localhost:8080/home/hello@Slf4j@RestControllerpublic class HomeController {@GetMapping("/home/{name}")public String test(@PathVariable("name") St
·
目录
-
restful风格的参数
http://localhost:8080/home/hello
@Slf4j
@RestController
public class HomeController {
@GetMapping("/home/{name}")
public String test(@PathVariable("name") String name){
return name;
}
}
-
普通类型的参数
http://localhost:8080/home?name=xiaoli
@Slf4j
@RestController
public class HomeController {
@GetMapping("/home")
public String test(@RequestParam(value = "name",defaultValue = "default",required = false) String name){
return name;
}
}
-
map类型的参数
http://localhost:8080/home?name=xiaoli&age=10
@Slf4j
@RestController
public class HomeController {
// 直接接受map
@GetMapping("/home")
public String test(@RequestParam Map<String,Object> params){
return "name:" + params.get("name") + "<br>age:" + params.get("age");
}
// 接受user实体的map
@GetMapping("/home1")
public String test(@SpringQueryMap User user){
return "";
}
}
-
数组类型的参数
http://localhost:8080/home?name=xiaoli&name=xiaohong
@Slf4j
@RestController
public class HomeController {
@GetMapping("/home")
public String test(@RequestParam("name") String [] names){
String result = "";
for(String name:names){
result += name + "<br>";
}
return result;
}
}
-
resetFul和集合
http://localhost:8088/1?idc=1&idc=2
http://localhost:8088/1?idc=1,2
@RequestMapping("/{a}")
public void aa(@PathVariable Integer a , @RequestParam(value = "idc",required = false) List<String> idc)
-
拒绝请求
@GetMapping(path = "/test",params = {"!name","age!=10"})
拒绝请求头中有name和age=10的请求。
更多推荐
已为社区贡献2条内容
所有评论(0)