![](/img/trans.png)
[英]Spring MVC - Is it possible to receive a strongly typed request object on a GET?
[英]Is it possible to have strongly typed HTTP request handlers in Restlet?
考慮以下ServerResource
派生類型:
public class UserResource extends ServerResource {
@Get
public User getUser(int id) {
return new User(id, "Mark", "Kharitonov");
}
}
(是的,無論給定的id總是返回相同的用戶)。
是否可以使其在Restlet中工作? 因為據我了解,GET處理程序的預期簽名是:
Representation get();
要么
Representation get(Variant v); // (no idea what it means yet)
現在我明白了,我可以實現非類型安全的GET處理程序以從請求中提取參數,然后調用getUser
,然后從結果中組成相應的Representation
實例並返回。 但這是一個樣板代碼,它不屬於應用程序代碼,它位於框架內部。 至少,這是通過OpenRasta(我在.NET中一直在使用的REST框架)完成的
謝謝。
您應該從簽名中刪除參數
@Get
public User getUser() {
String id = getQuery().getFirstValue("id");
return new User(id, "Mark", "Kharitonov");
}
在這種情況下,無需重寫get()方法,因為將自動檢測@Get批注。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.