HTTP是常用的B/S架构的前后端交互协议,springboot对http接口的传参有以下几种参数接收方式
@RequestMapping(path = "/test")
@ResponseBody
public String testProject(){
return "hello world!";
}
/**
*json提交数组,后台需要用list<T>来接收
*/
@PostMapping(path = "/json")
@ResponseBody
public String jsonParam(@RequestBody User user){
return user.toString();
}
/**
*pathVarible:参数需要经过urlencode编码才可被正常接收,参数可以接收多个
*/
@RequestMapping(path = "/path/{userName}",method = RequestMethod.GET)
@ResponseBody
public String pathParm(@PathVariable("userName") String userName){
return userName;
}
/**
*form-data:既可以提交键值对,又可以提交文件,且可以提交多个文件
*/
@ResponseBody
@RequestMapping(path = "/form")
public String formPram(@RequestParam String username, @RequestParam Integer age){
return username.concat(" : ").concat(String.valueOf(age));
}
/**
* x-www-form-urlencoded:该类型的提交方式只可以提交键值对,并且无需加上@RequestParam注解
*/
@ResponseBody
@RequestMapping(path = "/wwwform", method = RequestMethod.POST)
public String wwwFormPram(String username, Integer age) {
return "姓名:".concat(username).concat(",年龄: ").concat(String.valueOf(age));
}
@ResponseBody
@RequestMapping("/get")
public String getParam(String username, Integer age){
return "参数值:" + username + " : " + age;
}