不到1分钟解决了一个难题,你也能学会!

Internal Server Error: Missing URL template variable 'id' for method parameter of type Long的解决

编写SpringMVC rest api的时候发现这个问题.

Internal Server Error: Missing URL template variable 'id' for method parameter of type Long

类似的还有:

Internal Server Error: Missing URL template variable 'id' for method parameter of type Interger

Internal Server Error: Missing URL template variable 'id' for method parameter of type String

Internal Server Error: Missing URL template variable 'name' for method parameter of type String

Internal Server Error: Missing URL template variable 'password' for method parameter of type String

这类问题就是忘记进行类型转换吗?

不是,我们看到下面代码:

比如接口如下:

@RestController
public Error{
  @Autowired
  private UserService userService

  @GetMapping("")
  public User getUser(@PathVariable("id") Long id){
    userService.query(id)
  }
}

所以问题是,忘记加路径变量映射了。

编程要用正确的方法,不然解决不了问题。

解决方法:

@RestController
public Error{
  @Autowired
  private UserService userService

  @GetMapping("{id}")//就是这里CTO忘记加id了
  public User getUser(@PathVariable("id") Long id){
    userService.query(id)
  }
}

关注CTO,一起成功!

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值