2014-09-25 17 views
3

我使用Restlet框架來提供RESTful服務。如何自定義Restlet的錯誤消息

我用ResouceException返回HTTP錯誤代碼,如400錯誤請求,等等。

我用代碼,名稱和desc「新建」ResourceException(代碼,名稱,desc,uri)。我得到以下輸出:「錯誤的請求」

Bad Request (name) 

period is not a number (desc) 

You can get technical details here. (uri,though I didn't provide it) 

Please continue your visit at our home page. (home page, I didn't provide, too) 

我覺得最後兩句是用戶糾正沒有用。

我可以刪除它們嗎?怎麼做?或者如果我想輸出其他的東西,如何定製消息。

非常感謝。

回答

3

你可以用你自己的一個替換默認的StatusService

擴展類並覆蓋getRepresentation(Status, Request, Response)以提供您想要的格式。

這個就可以在你的Application使用setStatusService(StatusService)

可能有一個稍微最新的方式做到這一點,因爲我使用已經經歷了幾個的Restlet更新,現在的方法進行設置,但它仍然有效。

更新

爲的Restlet版本2.3.x版本 的方法重寫是toRepresentation(Status, Request, Response)

+0

爲什麼不實現這ResourceException或狀態構造函數,我認爲這將是方便開發者使用它。 – NingLee 2014-09-26 01:35:39

+0

@NingLee我實際上並不是Restlet團隊的成員,我可能已經給出了這種印象,我已經稍微修改了它,以消除這種含義。隨意問他們?但我不能說出他們的推理。 – Caleryn 2014-09-26 10:34:23