rest - 用于逻辑执行的正确 REST 方法

标签 rest spring-boot

我知道这是有争议的,但是什么是正确的 HTTP 方法,它只接受输入并执行逻辑并返回响应。

例如:如果我必须公开一个 REST 端点,它接受一个整数并返回一些数字系列?

最佳答案

正如 HTTP 协议(protocol)的 RFC ( https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html ) 中所述,HTTP 方法可以是幂等的,也可以不是:

Methods can also have the property of "idempotence" in that (aside from error or expiration issues) the side-effects of N > 0 identical requests is the same as for a single request. The methods GET, HEAD, PUT and DELETE share this property. Also, the methods OPTIONS and TRACE SHOULD NOT have side effects, and so are inherently idempotent.

因此,如果您的逻辑显着改变系统状态 - 您最好使用非幂等方法 - POST。如果通过调用服务方法在系统中的所有更改仅记录到日志文件 - 使用安全的 HTTP 方法,例如 GET

关于rest - 用于逻辑执行的正确 REST 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48612591/

相关文章:

c - 在不使用 libcurl 的情况下从 C 调用 REST API

java - 使用@Consumes、@Produces 和 JAXB 的简单 JAX-RS 示例

Java - long 显示错误值

java - 如何为函数中的带注释参数编写mockito

java - 设置springboot启动检查redis,database,mq

java - 带 @Secured 和 @PreAuthorize 注释的类不保护 super 方法

java - 将 JSON 响应解析为对象

php - MySQL : LEFT JOIN for 4 tables which are connected to each other

java - 如何扩展不可修改的模型以与 JPA 一起使用?

spring-boot - 为什么我的 spring-configuration-metadata.json 属性没有生成?