简体   繁体   English

REST 如何在没有完整实体的情况下发布或放置(@RequestBody)

[英]REST how to POST or PUT without full entity(@RequestBody)

I have an Entity 'Vote' that has fields:我有一个具有以下字段的实体“投票”:

private Integer id;
private LocalDate date;
private LocalTime time = LocalTime.now();
private User user;
private Restaurant restaurant;

I know that for REST POST peferably i shoud use resourse pattern like this:我知道对于 REST POST 最好我应该使用这样的资源模式:

/votes

and in case of update:并在更新的情况下:

/votes/{voteId}

Presumably i shoud receive full vote entity in my controller from frontend like this:大概我应该在我的 controller 从前端收到全票实体,如下所示:

@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Vote> create(@RequestBody Vote vote)

But i dont need that to create or update that entity, i only need restaurantId like this:但我不需要它来创建或更新该实体,我只需要这样的 restaurantId:

@PostMapping
public void create(@RequestParam int restaurantId) {
        voteService.create(SecurityUtil.getUserId(), restaurantId);
    }

So, will be the right choice to use resourse pattern like this, or i'm wrong?那么,使用这样的资源模式是正确的选择,还是我错了?

For POST create:对于 POST 创建:

/votes?restaurantId=10

For PUT update:对于 PUT 更新:

/votes/{voteId}?restaurantId=10

You should change it to:您应该将其更改为:

@PostMapping
public void create(@RequestParam("restaurantId") int restaurantId) {
    voteService.create(SecurityUtil.getUserId(), restaurantId);
}

You should mention the parameter name in @RequestParam("param_name")您应该在 @RequestParam("param_name") 中提及参数名称

Why not using path parameters?为什么不使用路径参数? It is a bit strange to pass get parameters to POST and PUT.将 get 参数传递给 POST 和 PUT 有点奇怪。

@PostMapping("/{restaurantId}")
public void createPost(@NonNull @PathVariable(value = "restaurantId") String restaurantId) {
    voteService.create(SecurityUtil.getUserId(), restaurantId);
}

@PutMapping("/{restaurantId}")
public void createPut(@NonNull @PathVariable(value = "restaurantId") String restaurantId) {
    voteService.create(SecurityUtil.getUserId(), restaurantId);
}

So, will be the right choice to use resourse pattern like this, or i'm wrong?那么,使用这样的资源模式是正确的选择,还是我错了?

"It depends". “这取决于”。

The spelling of the URI doesn't matter to the machines; URI 的拼写与机器无关。 /0d905ca3-c848-4bc5-bc4a-658fbec88ab5 is a perfectly fine path to use. /0d905ca3-c848-4bc5-bc4a-658fbec88ab5是一个非常好的使用路径。 Human readable spellings are just a convenience for humans - a "RESTful" spelling like /votes isn't any more REST than using the UUID, but when the operators are pouring through logs trying to identify a traffic pattern, they will probably be happier with the second choice.人类可读的拼写只是对人类的一种便利——像/votes之类的“RESTful”拼写与使用 UUID 相比不再是 REST,但是当操作员翻阅日志试图识别流量模式时,他们可能会更高兴第二个选择。

POST, as a method token, is fine. POST,作为方法标记,很好。 See It's Okay to Use POST (Fielding, 2009).请参阅使用 POST 没问题(菲尔丁,2009 年)。

PUT , however, doesn't quite sound like what you want.但是, PUT听起来并不像您想要的那样。 Semantically, PUT means "replace the current representation of the resource with payload of the current message."从语义上讲, PUT的意思是“用当前消息的有效负载替换资源的当前表示”。 It's a remote authoring method, which when paired with GET gives you a document store:这是一种远程创作方法,与GET搭配使用时,可为您提供文档存储:

PUT is the method you would use on the world wide web to edit your home page, for example.例如, PUT是您将在全球范围内使用 web 来编辑您的主页的方法。

GET /home-page

(make changes locally)

PUT /home-page

And the body of that PUT request is the intended new version of the home page.该 PUT 请求的正文是主页的预期新版本。

In your case:在你的情况下:

PUT /votes/{voteId}?restaurantId=10

is supposed to have a payload that looks like what you would read if you did应该有一个有效载荷,看起来就像你读到的那样

GET /votes/{voteId}?restaurantId=10

It needs to be a complete representation of the resource.它需要是资源的完整表示。

That doesn't mean it needs to be a complete representation of the entity ;并不意味着它需要是实体的完整表示; resources are a generalization of a document;资源是文档的概括; a view into your entity's data.查看您的实体数据。 Remote authoring sends a new representation of the view, and it's the server's job to make the matching changes to the entity.远程创作发送视图的新表示,并且对实体进行匹配更改是服务器的工作。 See Jim Webber 2011 .参见2011 年的吉姆·韦伯

If you don't want to make your updates that way, that's fine -- but then you shouldn't be using PUT , as that defeats the point of having a common semantic with all other resources (you should use POST).如果您不想以这种方式进行更新,那很好——但是您不应该使用PUT ,因为这违背了与所有其他资源具有共同语义的意义(您应该使用 POST)。

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

相关问题 如何确保维护 POST REST API 的 @RequestBody 中地图元素的顺序? - How to make sure order of map elements inside a @RequestBody of POST REST API is maintained? Spring MVC:RequestBody + ResponseEntity:如何在没有某些obj字段的情况下返回对象/实体 - Spring MVC: RequestBody + ResponseEntity: How to return an object/entity without some of the obj fields 如果 RequestBody 中存在任何未知字段,则需要拒绝 POST/PUT 请求 - Need to Reject POST/PUT request if any unknown field is present in RequestBody 在REST API中创建PUT和POST端点,而不创建GET端点? - Create PUT and POST endpoints in a REST API, without creating a GET endpoint? Spring Rest POST Json RequestBody 内容类型不支持 - Spring Rest POST Json RequestBody Content type not supported Spring Rest在@RequestBody中添加@NotEmpty而不创建请求POJO - Spring Rest Add @NotEmpty in @RequestBody without creating request POJO 如何在不指定内容类型的情况下将 JSON POST/PUT 到 REST API 端点:application/json header - How do I POST/PUT JSON to a REST API endpoint without specifying Content-Type: application/json header 如何在地图上发布/放置 <String,Entity> 收藏协会 - How to POST/PUT on a Map<String,Entity> collection association 使用@POST或@PUT使用@QueryParam - Rest using @QueryParam with @POST or @PUT @RequestBody没有使用Rest服务 - @RequestBody not working on Rest service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM