使用RESTEasy,我已經實現了Application的子類來提供單例資源列表。有沒有辦法在以後動態添加另一個單身人士?我還沒有找到從API文檔中找到它的方法。RESTEasy - 動態添加資源類
1
A
回答
1
我還沒有嘗試過這個自己,但我發現這個地方,描述了博客文章: http://sarbarian.wordpress.com/2010/03/07/resteasy-and-osgi-perfect-match/
在部署過程中,提出的RESTEasy它在servlet上下文註冊表。在博客中提出的想法是,您從servlet上下文獲取註冊表,然後添加您的資源類。
事情是這樣的:
import org.jboss.resteasy.spi.Registry;
Object resource = new MyService();
Registry registry = (Registry) context.getAttribute(Registry.class.getName());
registry.addSingletonResource(resource);
1
我已經嘗試過這個自己,儘管這是傷腦筋,它的偉大工程。我們有一個基礎設施,其中有幾個獨立的項目(插件)由我們的主應用程序導入和加載,我們希望在我們現有的RestEasy API(和Swagger文檔)中包含對它們的訪問。
我們最初將類加載到主Rest應用程序類中,這需要我們的API隨時顯示所有插件。爲了避免這種情況並重新設置這些項目的獨立性(在任何給定時間都可以包含全部,部分或全部),我們創建了動態方法來返回特性/目標類本身,它在自舉階段加載RestEasy框架。它像一個魅力。
在我們的REST應用類,我們實現了它如下:
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<>();
classes.add(mainAPIStuff.class);
classes.add(plugin1.class);
classes.add(plugin2.class);
classes.add(plugin3.class);
.
.
.
return classes;
}
那是老樣子。新的辦法是通過我們的所有插件的循環,並返回該插件提供的類的一個HashSet:
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<>();
classes.add(mainAPIStuff.class);
classes.addAll(pluginsHelper.getClasses());
return classes;
}
的pluginsHelper有各式各樣的共享插件的方法和管理工具,像getClasses(),這確實基本上是這樣的:
public static Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<>();
// Probe plugins for their Rest Class implementation
List plugins = getCurrentlyInstalledPluginsFromOurSystem();
for (PluginObject plugin : plugins) {
Class<?> clazz = plugin.getPluginRestClass();
if (clazz != null) {
classes.add(clazz);
}
}
return classes;
}
RestEasy的構建目標休息時動態任何目標被調用,所以所有的插件安裝,啓動,配置已經完成,系統處於正確狀態時,它被調用之前。這是非常有價值的理解,因爲那樣你就可以在通話時完成RestEasy類的所有操作和加載。
0
我在嵌入式環境中,我手工實例ResteasyDeployment
,並註冊資源類使用Reateasy與暗潮是很容易的,看到的代碼片段的打擊:
UndertowJaxrsServer undertowJaxrsServer = new UndertowJaxrsServer();
ResteasyDeployment resteasyDeployment = new ResteasyDeployment();
undertowJaxrsServer.start();
resteasyDeployment.start();
final DeploymentInfo undertowDeployment =
undertowJaxrsServer
.undertowDeployment(resteasyDeployment)
.setContextPath("/abc")
.setDeploymentName("TEST")
.setClassLoader(Thread.currentThread().getContextClassLoader());
undertowJaxrsServer.deploy(undertowDeployment);
resteasyDeployment.getRegistry().addSingletonResource(new ResourceApiImpl());
相關問題
- 1. Eclipse:添加源動態添加類
- 2. 動態添加資源字符串
- 3. 將動態操作添加到$資源
- 4. 動態添加新資源 - CoAP
- 5. WPF添加動態資源programmically C#
- 6. 動態資源加載Android
- 7. Android動態加載資源
- 8. RESTEasy除子資源以外的JAX-RS動態調度
- 9. 動態資源
- 10. 從C#中的資源動態添加和加載圖像
- 11. 添加MapRoute的靜態資源ASP.NET MVC
- 12. 如何向vaadin添加靜態資源
- 13. 灰熊2.2添加靜態資源
- 14. 動態資源Angularjs
- 15. 資源動態段
- 16. 靜態資源的JSF加載資源
- 17. Codedom添加資源
- 18. 如何向類路徑添加資源
- 19. SpringMVC:向靜態資源添加媒體類型
- 20. 如何手動將資源添加到Win32資源文件
- 21. WPF動態加載資源字典
- 22. 如何動態加載R.styleable資源?
- 23. 動態加載WPF衛星資源
- 24. 動態加載資源文件
- 25. 動態加載apk使用資源
- 26. 如何更新動態資源中的動態資源?
- 27. 動態添加縮小的嵌入式資源
- 28. zend_acl:動態添加額外資源並獲取參數predispatch
- 29. 動態添加或修改字符串資源
- 30. Android的動態添加項陣列XML資源