rest - 使用springboot为@PathParam获取null
问题描述
我是 Web 服务的新手,正在使用 spring-boot 创建 Web 服务,而在使用http://localhost:8085/user/30?name=abc发出请求时,我的 id 属性为空。
@GetMapping(value="{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public String getUser(@PathParam("id") Long id,
@QueryParam("name") String name){
System.out.println(" Got id by path param : "+ id + " And Got name using Query Param " +name);
return " Got id by path param : "+ id + " And Got name using Query Param " +name;
}
编辑以添加屏幕截图。
截取自 Postman 提前致谢。
解决方案
您需要使用@PathVariable
,因为您使用的spring-rest
不是注释@PathParam
JAX-RS
@GetMapping(value="{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public String getUser(@PathVariable("id") Long id,
@QueryParam("name") String name){
System.out.println(" Got id by path param : "+ id + " And Got name using Query Param " +name);
return " Got id by path param : "+ id + " And Got name using Query Param " +name;
}
推荐阅读
- angular - 数组值被最新值 angular 7 覆盖
- r - 带有长度函数的 if 语句
- python - Python将时间间隔划分为每小时的桶
- docker - 我有 dockerVolume,它有一个 json 文件。我希望使用一些 docker 命令或运行 shell 脚本来打印 json
- android - 在 Dagger 2 中使用 costom 范围有什么好处
- python - 添加段落文本时如何使用python-docx删除Word标题中的新行?
- rasa-nlu - Rasa 聊天机器人默认实体值(如果未指定)
- java - 更合适的方法找到具有特定值的实例的索引
- c - 关于main函数中scanf问题的问题
- python - 如何从 Colab 中的子目录导入