2
對所有REST服務總是使用Response作爲返回類型是一個好主意。 例如,假設我有一個獲得所有客戶REST方法返回類型 - 最佳實踐
First way:
public List <customer> getAllCustomers(){
// select all customers from database
}
Second Way:
public Response getAllCustomers(){
// user Response.ResponseBuilder.entity(customer)
}
enter code here
這是一個更好的方法的方法。所有方法都可以返回Response作爲返回類型(如第二種方式)。
請閱讀[this](http://programmers.stackexchange.com/questions/211275/should-an-http-api-always-return-a-body)。看來這方面的規格有點薄。 –
響應允許您設置狀態碼,標題等。您無法通過點擊「HttpServletResponse」(這很醜陋)或使用過濾器(在考慮所有可能的資源時很複雜)來返回模型對象。在簡單的GET情況下,響應始終爲200或404(我們可以拋出異常),這是合理的,可以返回模型對象,但使用其他方法響應更容易。就我個人而言,我更願意保持一致並始終返回響應。 –