java 当它应该返回 400 Bad Request 时,Restlet 是否返回 415 Unsupported Media Type?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4308672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:47:08  来源:igfitidea点击:

Is Restlet returning 415 Unsupported Media Type when it should return 400 Bad Request?

javarestcontent-typeHymansonrestlet-2.0

提问by Matteo Caprari

I'm using Restlet 2.1 with Hymanson to build a json REST api.

我正在使用 Restlet 2.1 和 Hymanson 来构建一个 json REST api。

When I make a request with the expected content type but a malformed body, I get back a 415 "Unsuppored Media Type" status code. I think the correct error code should be 400 "Bad Request".

当我使用预期的内容类型但格式不正确的请求发出请求时,我会返回 415“不支持的媒体类型”状态代码。我认为正确的错误代码应该是 400“Bad Request”。

Apparently the mixup happens when Hymanson tries and fails to decode the garbage.

显然,当Hyman逊尝试解码垃圾但未能成功时,就会发生混淆。

I'll try to make the case more clear with some code:

我将尝试使用一些代码使案例更加清晰:

// java method mapping
@Post("json")
public Project create(Project project) {

The service invocation with curl

使用 curl 的服务调用

$ curl -i -XPOST -H 'content-type: application/json' -d '{xgarbage}' http://localhost:8080/projects HTTP/1.1 415 Unsupported Media Type

And a fragmente of the stack trace os recorde in the logs:

以及日志中记录的堆栈跟踪操作系统的片段:

Nov 29, 2010 9:51:56 PM org.restlet.ext.Hymanson.HymansonRepresentation getObject
WARNING: Unable to parse the object with Hymanson.
org.codehaus.Hymanson.JsonParseException: Unexpected character ('x' (code 120)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: java.io.ByteArrayInputStream@693e4a5a; line: 1, column: 2]
at org.codehaus.Hymanson.JsonParser._constructError(JsonParser.java:929)

The actual implementation of the service is never hit, so somewhere a decision is made to map the garbled content to a 415.

服务的实际实现永远不会命中,因此在某处做出决定将乱码内容映射到 415。

Now, my question is: is this correct? If I'm reading correctly the following quotes from the book "RESTful Web Services", it is not, but I'm open to corrections.

现在,我的问题是:这是正确的吗?如果我正确地阅读了“RESTful Web 服务”一书中的以下引述,事实并非如此,但我愿意接受更正。

[400 Bad Request] It's commonly used when the client submits a representation along with a PUT or POST request, and the representation is in the right format, but it doesn't make any sense.

[400 Bad Request] 当客户端随PUT或POST请求提交一个表示时,通常使用它,并且该表示的格式正确,但没有任何意义。

.

.

[415 Unsupported Media Type] If the client sends a document that's got the right media type but the wrong format (such as an XML document written in the wrong vocabulary), a better response is the more generic 400 (“Bad Request”)

[415 Unsupported Media Type] 如果客户端发送的文档具有正确的媒体类型但格式错误(例如使用错误词汇表编写的 XML 文档),更好的响应是更通用的 400(“Bad Request”)

Right or wrong, I'd prefer to return a 400.

不管是对是错,我宁愿退回 400。

Is there a way to change the behaviour without renouncing at the auto-magic serialisation provided by Hymanson?

有没有办法在不放弃Hyman逊提供的自动魔法序列化的情况下改变行为?

Any help is greatly appreciated, thanks!

非常感谢任何帮助,谢谢!

回答by Matteo Caprari

415 is correct, as the request is NOTin the right format if it is corrupted in anyway. For example non-parseable JSON or XML. Malformed JSON or XML is NOTJSON or XML by definition, and thus is an unsupported media type, there is no way for Hymanson to know that is is supposed to be JSON, it just knows that it isn't JSON that it can parse.

415 是正确的,因为如果请求以任何方式损坏,则请求的格式正确。例如不可解析的 JSON 或 XML。根据定义,格式错误的 JSON 或 XML不是JSON 或 XML,因此是不受支持的媒体类型,Hymanson 无法知道它应该是 JSON,它只知道它不是可以解析的 JSON。

The offical documentationis really clear about this.

官方文档对此非常清楚。

10.4.16 415 Unsupported Media Type

10.4.16 415 不支持的媒体类型

The server is refusing to service the request because the entity of the request is in a format not supported by the requested resource for the requested method.

服务器拒绝为请求提供服务,因为请求实体的格式不为请求方法的请求资源所支持。

you said, hey this is JSON, and it isn't so the server says, hey, what I got isn't JSON and not supported by this resource.

你说,嘿,这是 JSON,但服务器不是这样说的,嘿,我得到的不是 JSON,并且不受此资源支持。