今天学习SpringBoot传参时候又犯了一些错误。这里我给大家展示出来
前台代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
-----------------------------------------------paramtypetest-----------------------------------------------<br>
<a href="car/3/owner/lisi">car/{id}/owner/{username}</a>
</body>
</html>
后台controller代码
@RestController
public class paramtypetestController {
@RequestMapping("car/{id}/owner/{username}")
public Map<String,Object> requesttest(@PathVariable("id") Integer id,
@PathVariable("owner") String username,
@PathVariable Map<String,String> pv){
Map<String,Object> map=new HashMap();
map.put("id",id);
map.put("owner",username);
map.put("pv",pv);
return map;
}
}
执行结果
我们查看错误问题描述,
说我们参数类型为String的owner没有提供
查看后台代码,我们传参使用的是id跟username
car跟owner是表述我们操作的对象的,修改后台代码
@RestController
public class paramtypetestController {
@RequestMapping("car/{id}/owner/{username}")
public Map<String,Object> requesttest(@PathVariable("id") Integer id,
@PathVariable("username") String username,
@PathVariable Map<String,String> pv){
Map<String,Object> map=new HashMap();
map.put("id",id);
map.put("owner",username);
map.put("pv",pv);
return map;
}
}
运行
成功