简体   繁体   English

当Restlet返回400 Bad Request时,它是否返回415不支持的媒体类型?

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

I'm using Restlet 2.1 with jackson to build a json REST api. 我正在使用Restlet 2.1与jackson构建一个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. 当我使用预期的内容类型但是格式错误的主体发出请求时,我会收到415“Unsuppored Media Type”状态代码。 I think the correct error code should be 400 "Bad Request". 我认为正确的错误代码应为400“错误请求”。

Apparently the mixup happens when Jackson tries and fails to decode the garbage. 显然,当杰克逊尝试并且无法解码垃圾时,混合就会发生。

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.jackson.JacksonRepresentation getObject
WARNING: Unable to parse the object with Jackson.
org.codehaus.jackson.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.jackson.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 Services”一书中的以下引用,那就不是,但我愿意接受更正。

[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错误请求]它通常在客户端提交表示以及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不支持的媒体类型]如果客户端发送的文档具有正确的媒体类型但格式错误(例如用错误的词汇表编写的XML文档),则更好的响应是更通用的400(“错误请求”)

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 Jackson? 有没有办法改变行为而不放弃杰克逊提供的自动魔术序列化?

Any help is greatly appreciated, thanks! 非常感谢任何帮助,谢谢!

415 is correct, as the request is NOT in the right format if it is corrupted in anyway. 415是正确的,因为如果请求无论如何都被破坏,请求的格式正确。 For example non-parseable JSON or XML. 例如,不可解析的JSON或XML。 Malformed JSON or XML is NOT JSON or XML by definition, and thus is an unsupported media type, there is no way for Jackson to know that is is supposed to be JSON, it just knows that it isn't JSON that it can parse. 根据定义,格式错误的JSON或XML 不是 JSON或XML,因此是不受支持的媒体类型,杰克逊无法知道应该是JSON,它只知道它可以解析它不是JSON。

The offical documentation is 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而且不受这个资源的支持。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM