ColdFusion 10 REST API:如何在没有RestSetResponse()的情况下设置状态代码201 [英] ColdFusion 10 REST API: How to set status code 201 without RestSetResponse()
问题描述
我正在使用ColdFusion 10的新内置REST API,我想返回状态代码201(已创建)。我首先尝试了在此处描述的RestSetResponse()方法: http:/ /www.adobe.com/devnet/coldfusion/articles/restful-web-services.html 。除了强制您将函数的returntype设置为 void之外,它的效果很好。 void的问题在于,每当我抛出异常时,它就不再返回正确的JSON错误消息。
I'm using ColdFusion 10's new built-in REST API and I'd like to return a status code of 201 (Created). I first tried the RestSetResponse() approach that's described here: http://www.adobe.com/devnet/coldfusion/articles/restful-web-services.html. It works well, except that it forces you to set the function's returntype to "void". The problem with "void" is that whenever I throw an exception, it no longer returns the proper JSON error message.
抛出此异常:
<cfthrow errorcode="400" message="Validation error." />
当returntype为 struct时,返回格式正确的JSON:
Returns a nicely formatted JSON when the returntype is "struct":
HTTP/1.1 400 Bad Request
Content-Type: application/json
{"Message":"Validation error."}
但是当returntype为 void时(使用RestSetResponse()是必需的)
But when the returntype is "void" (which is required to use RestSetResponse(), the response is some ugly HTML response.
因此,我不得不恢复使用returntype struct,放弃了RestSetResponse()并尝试了此操作:
Because of this, I had to revert to using returntype "struct", gave up on RestSetResponse(), and tried this:
<cfheader statusCode="201" statusText="Created" />
但是它不起作用,似乎ColdFusion会覆盖statusCode并在成功时始终返回200(确定)。状态代码为201,而未将函数的返回类型设置为 void?
But it doesn't work. It seems that ColdFusion overwrites the statusCode and always returns 200 (OK) when it's successful. Anyone know of a way to change the status code to 201 without setting the returntype of the function to "void"?
推荐答案
我看不出为什么 restSetResponse()
应该要求返回类型为v oid,但已验证,如果不是这种情况,它将被忽略。
I can't see a good reason why restSetResponse()
should require a returntype of void, but have verified it is ignored if this is not the case. Which is a bit rubbish.
我唯一可以通过解决您的情况的方法就是滚动自己的结构,其中包含错误详细信息,然后将其用作为 restSetResponse()
调用设置的 content
值。
The only thing I can think by way of working around your situation is to roll-your-own struct with the error detail in it, then use that as the content
value set for the restSetResponse()
call.
这有点像杰瑞构建的,但是在这种情况下,我认为您受ColdFusion杰瑞构建的约束。
This is a bit jerry-built, but you're constrainted by the jerry-built-ness of ColdFusion in this instance, I think.
我've 记录了与此相关的错误。
这篇关于ColdFusion 10 REST API:如何在没有RestSetResponse()的情况下设置状态代码201的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!