首页 > 解决方案 > 使用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 提前致谢。

标签: restweb-servicesspring-boot

解决方案


您需要使用@PathVariable,因为您使用的spring-rest不是注释@PathParamJAX-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;
}

推荐阅读