【问题标题】:How to use findById() in Spring Boot?如何在 Spring Boot 中使用 findById()?
【发布时间】:2019-04-28 09:30:08
【问题描述】:

我已经尝试过这段代码,但它不断地给我一个错误。

      @GetMapping("/findbyid/{id}")
      public Optional<UserDTO> getUserByID(@PathVariable Integer id){
       return userservices.finBYID(id);
      }




     @Override
     public Optional<UserDTO> finBYID(Integer id) {

    return userrepository.findById(id);
     }

结果是:

  {
   "timestamp": "2019-04-28T04:53:00.225+0000",
   "status": 404,
   "error": "Not Found",
   "message": "No message available",
   "path": "/user/find/1"
   }

【问题讨论】:

  • 响应中的路径与您在映射中设置的不匹配。请仔细检查您是否向正确的端点发出请求,然后提供minimal reproducible example。请注意,传统的 RESTful API 看起来像 /users/{id}

标签: java maven spring-boot


【解决方案1】:

根据代码,请求 URI 应该是'/findbyid/{id}',但它是'/find/1'。

【讨论】:

  • 你磁带的路径与@GetMapping("/findbyid/{id}")不匹配,首先检查用户是否存在然后URI应该像/findbyid/{id}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-14
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多