简体   繁体   English

使用spring-boot和spring-data的正确PUT方法

[英]Correct PUT method with spring-boot and spring-data

I'm using Spring Boot and Spring Data to build RESTful API. 我正在使用Spring Boot和Spring Data构建RESTful API。 I have two entities: Site and Floor . 我有两个实体: SiteFloor There are several Floors in one Site . 一个Site有多个Floors I'm wondering what is the correct way to make a PUT method? 我想知道什么是制作PUT方法的正确方法? So far I have this method in SiteController.class : 到目前为止,我在SiteController.class具有以下方法:

@RequestMapping(method = RequestMethod.PUT, value = "/api/sites")
    public void updateSite(@RequestBody Site site){
        sitesService.updateSite(site);
    }

And this is SitesService.class : 这是SitesService.class

public void updateSite(Site site) {
        sitesRepository.save(site);
    }

Model Site.class has id and name . 模型Site.class具有idname

When I send following JSON with PUT method to /api/sites ... 当我将以下带有PUT方法的JSON发送到/api/sites ...

{
  "id": 1,
  "name": "SITE 1"
}

...I get update updated Site entity as expected. ...我得到了预期的更新更新的Site实体。

But I'm wondering whether this implementation is right or not? 但是我想知道这种实现是否正确? Shouldn't I specify an id of the Site in URI that I'm going to update with PUT? 我是否应该在URI中指定要使用PUT更新的站点ID? Like this: /api/sites/{id} ? 像这样: /api/sites/{id}

我认为,您可以使用/api/sites/{id}发送PUT请求,在控制器方法中,可以使用id从存储库中获取实体,更改属性“ name”,然后将其保存在库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM