gpt4 book ai didi

java - 发送 DELETE 时实现状态 200

转载 作者:行者123 更新时间:2023-11-29 10:08:42 25 4
gpt4 key购买 nike

我想用 Spring Boot 实现 DELETE 请求。我试过这个:

@DeleteMapping("remove/{id}")
public ResponseEntity<?> remove(@PathVariable String id) {
return ResponseEntity.ok(transactionNotesService.delete(Long.valueOf(id)))
.orElseGet(() -> notFound().build());
}

但我得到 The method ok(T) in the type ResponseEntity is not applicable for the arguments (void)

如何修改代码以在执行 DELETE 时返回代码 200?

最佳答案

根据HTTP Spec你应该总是有一个状态为 200 的 body 。

10.2.1 200 OK

The request has succeeded. The information returned with the response is dependent on the method used in the request, for example:

GET an entity corresponding to the requested resource is sent in the response;

HEAD the entity-header fields corresponding to the requested resource are sent in the response without any message-body;

POST an entity describing or containing the result of the action;

TRACE an entity containing the request message as received by the end server.

您应该改用状态 204(无内容),它不接受正文参数。

10.2.5 204 No Content

The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant.

If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view.

The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.

transactionNotesService.delete(Long.valueOf(id));
return ResponseEntity.noContent().build();

关于java - 发送 DELETE 时实现状态 200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54438313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com