【问题标题】:Getting null with @pathparam and @requestmapping使用 @pathparam 和 @requestmapping 获取 null
【发布时间】:2017-05-30 04:08:11
【问题描述】:

我正在使用 spring-boot 1.4.3.RELEASE 来创建 Web 服务,而在使用 http://localhost:7211/person/get/ram 发出请求时,我的 id 属性为空

@RequestMapping(value="/person/get/{id}", method=RequestMethod.GET, produces="application/json")
    public @ResponseBody Person getPersonById(@PathParam("id") String id) {
        return personService.getPersonById(id);
    }

你能建议我吗,我有什么遗漏的吗?

【问题讨论】:

  • "@PathParam" 是 JAX-RS 注释,用于类似 Jersey 的实现。 Spring 等效注解是“@PathVariable”。

标签: spring-boot


【解决方案1】:

获取路径变量的注解是@PathVariable。看起来您使用了 @PathParam ,这是不正确的。

查看更多详情:

requestparam-vs-pathvariable

【讨论】:

  • 不客气..您能否选择答案为“正确”。谢谢。
【解决方案2】:

正如上面已经提到的答案应该使用@PathVariable,我想清除@PathVariable 和@PathParam 之间的混淆

大多数人对此部分感到困惑,因为 Spring 和 Jersey 等其他 REST 实现对同一事物使用截然不同的注释

@QueryParam in Jersey@RequestParam in Spring Rest API

@PathParam in Jersey@PathVariable in Spring Rest API

【讨论】:

    【解决方案3】:

    使用@PathVariable 注解代替@PathParam。

    【讨论】:

      【解决方案4】:

      id 应该是 Long 而不是 String @PathVariable。如果是这样,那么...

      @RequestMapping(value="/person/get/{id}", method=RequestMethod.GET, produces="application/json")
      public @ResponseBody Person getPersonById(@PathVariable("id") Long id) {
          return personService.getPersonById(id);
      }
      

      【讨论】:

        猜你喜欢
        • 2019-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-27
        • 1970-01-01
        • 2011-04-10
        相关资源
        最近更新 更多