6

ColdFusion 10 の新しいビルトイン REST API を使用しており、ステータス コード 201 (作成済み) を返したいと考えています。ここで説明されている RestSetResponse() アプローチを最初に試しました: http://www.adobe.com/devnet/coldfusion/articles/restful-web-services.html。関数の returntype を「void」に設定する必要があることを除いて、これはうまく機能します。「void」の問題は、例外をスローするたびに、適切な JSON エラー メッセージが返されなくなることです。

この例外をスローする:

<cfthrow errorcode="400" message="Validation error." />

returntype が「struct」の場合、きれいにフォーマットされた JSON を返します。

HTTP/1.1 400 Bad Request
Content-Type: application/json

{"Message":"Validation error."}

しかし、returntype が "void" (RestSetResponse() を使用するために必要) の場合、応答は見苦しい HTML 応答になります。

このため、returntype "struct" の使用に戻さなければならず、RestSetResponse() をあきらめ、これを試しました。

<cfheader statusCode="201" statusText="Created" />

しかし、うまくいきません。ColdFusion は statusCode を上書きし、成功すると常に 200 (OK) を返すようです。関数の returntype を「void」に設定せずにステータスコードを 201 に変更する方法を知っている人はいますか?

4

1 に答える 1

1

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.

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.

This is a bit jerry-built, but you're constrainted by the jerry-built-ness of ColdFusion in this instance, I think.

I've logged a bug relating to this.

于 2013-03-31T01:35:32.207 に答える