(快速參考)

回應

目的

嘗試傳回最適合所要求內容類型 (由 Accept 標頭或檔案副檔名指定) 的類型

範例

// pick the best content type to respond with
respond Book.get(1)

// pick the best content type to respond with from the given formats
respond Book.get(1), formats: ['xml', 'json']

說明

respond 方法使用 內容協商 回應最適合的內容類型。請參閱 REST 支援 中的文件以取得更多資訊。

如果選擇檢視進行呈現,則會自動計算模型以傳遞至檢視。

下表總結此慣例

範例 引數類型 計算的模型變數

respond Book.list()

java.util.List

bookList

respond Book.get(1)

example.Book

book

respond( [1,2] )

java.util.List

integerList

respond( [1,2] as Set )

java.util.Set

integerSet

respond( [1,2] as Integer[] )

Integer[]

integerArray

參數

  • object - 要呈現的物件

  • arguments - 命名引數。

支援的 arguments

  • view - 在 HTML 呈現時要使用的檢視

  • model - 在 HTML 呈現時要使用的模型

  • status - 回應狀態

  • formats - 回應格式清單

  • includes - 使用轉換器 API 呈現時要包含的屬性

  • excludes - 使用轉換器 API 呈現時要排除的屬性

由於 Groovy 的運作方式,在使用 Map 和引數回應時,以這種方式回應 respond([:], status: 200) 很重要,而不是這種方式 respond([:], [status: 200])。否則會導致引數顛倒。