- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在将图像发布到我的服务器(使用 Java 和 Spring)。
可能会发生一些事情:
200
和图像 ID。SaveFileException
)。 DatabaseException
)。IOException
。 简而言之,我无法保存/创建图像。
我应该返回什么 HTTP 代码?
根据Exception
是否应该返回多条代码和多条消息?
最佳答案
首先,让我强调一下,状态码是为了指示服务器尝试理解和满足客户端请求的结果。
If everything goes well, then I return
200
and the image id.
似乎没问题,但我建议您返回 201
以及 Location
header 代替。引用 RFC 7231关于 POST
方法:
If one or more resources has been created on the origin server as a result of successfully processing a
POST
request, the origin server SHOULD send a201
(Created) response containing aLocation
header field that provides an identifier for the primary resource created and a representation that describes the status of the request while referring to the new resource(s).
与 201
一起,Location
header 用于指示新创建的资源所在的位置。如果未提供 Location
header ,则客户端应假定该资源由有效请求 URI 标识:
The
201
(Created) status code indicates that the request has been fulfilled and has resulted in one or more new resources being created. The primary resource created by the request is identified by either aLocation
header field in the response or, if noLocation
field is received, by the effective request URI. [...]
客户端能否执行新请求并解决问题?如果是这样,请在 4xx
范围内选择一个状态代码:
The
4xx
(Client Error) class of status code indicates that the client seems to have erred. Except when responding to aHEAD
request, the server SHOULD send a representation containing an explanation of the error situation, and whether it is a temporary or permanent condition. These status codes are applicable to any request method.
Michael Kropat放在一起很有用set of flowcharts这可能会给你一些见解。请参阅下表以确定最合适的 4xx
状态代码:
根据导致错误的原因,一些有效的选项是:
The
413
(Payload Too Large) status code indicates that the server is refusing to process a request because the request payload is larger than the server is willing or able to process. [...]
6.5.13. 415 Unsupported Media Type
The
415
(Unsupported Media Type) status code indicates that the origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource. The format problem might be due to the request's indicatedContent-Type
orContent-Encoding
, or as a result of inspecting the data directly.
The
400
(Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
如果错误是由服务器引起的,则 5xx
范围内的状态代码将是准确的:
The
5xx
(Server Error) class of status code indicates that the server is aware that it has erred or is incapable of performing the requested method. Except when responding to aHEAD
request, the server SHOULD send a representation containing an explanation of the error situation, and whether it is a temporary or permanent condition.
请看下面的流程图:
我会建议 500
:
6.6.1. 500 Internal Server Error
The
500
(Internal Server Error) status code indicates that the server encountered an unexpected condition that prevented it from fulfilling the request.
关于java - 当没有创建资源时,我应该为 POST 返回什么 HTTP 状态代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55685576/
我正在使用 Gunicorn 为 Django 应用程序提供服务,它工作正常,直到我将其超时时间从 30 秒更改为 900000 秒,我不得不这样做,因为我有一个用例需要上传和处理一个巨大的文件(过程
我有一个带有非常基本的管道的Jenkinsfile,它可以旋转docker容器: pipeline { agent { dockerfile { args '-u root' } } stag
在学习 MEAN 堆栈的过程中,我遇到了一个问题。每当我尝试使用 Passport 验证方法时,它都不会返回任何响应。我总是收到“localhost没有发送任何数据。ERR_EMPTY_RESPONS
在当今的大多数企业堆栈中,数据库是我们存储所有秘密的地方。它是安全屋,是待命室,也是用于存储可能非常私密或极具价值的物品的集散地。对于依赖它的数据库管理员、程序员和DevOps团队来说,保护它免受所
是否可以创建像图片上那样的边框?只需使用 css 边框属性。最终结果将是没 Angular 盒子。我不想添加额外的 html 元素。我只想为每个 li 元素添加 css 边框信息。 假设这是一个 ul
我是一名优秀的程序员,十分优秀!