我正在尝试创建一个 Grizzly 网络服务引擎,但缺少一些元素。
这是我想做的:
将 Grizzly 服务器创建为可执行 jar(这没问题)
此服务器必须能够查看 Web 服务存档的文件夹(包含在 jar 或 war 或其他文件中)
服务器应公开此 Web 服务。
我已经知道的:
Web 服务可以构建为 servlet(似乎 Grizzly 能够运行 servlet,但我没有找到关于如何加载包含在“.war”或其他文件中的外部 serlvet 的任何信息)
我尝试使用 ClassLoader 加载在“.jar”中导出的 JAX-RS2 网络服务,但 @path 绑定(bind)不起作用(我可能遗漏了一些东西)
我想知道的:
我应该为 Web 服务存档使用哪种类型的存档?
Web 服务应该是 servlet 吗?在这些情况下,如何使用 Grizzly 动态加载 servlet?
我走错方向了吗?
这是我对类加载器所做的
File file = new File("c:\\Users\\User\\Desktop\\myresource.jar");
URL url = file.toURI().toURL();
URL[] urls = new URL[]{url};
ClassLoader cl = new URLClassLoader(urls);
URLClassLoader child = new URLClassLoader (urls, this.getClass().getClassLoader());
Class<?> classToLoad = Class.forName ("Test.ExternalWS.MyResource", true, child);
Method method = classToLoad.getDeclaredMethod ("getIt");
Object instance = classToLoad.newInstance ();
Object result = method.invoke (instance);
this.WSInstance = classToLoad.newInstance();
这是网络服务
@Path("我的资源") 公共(public)类 MyResource {
@GET @Produces(MediaType.TEXT_PLAIN) public String getIt() { return "Got it!"; }
当我尝试访问 localhost:8080/....../myresource 时没有任何反应。
我尝试了所有可能的 url 并设置了一个断点......从未到达。
最佳答案
不确定您使用的是哪个 Jersey 版本。 在 Jersey 2 中,我是这样工作的:
File file = new File("/path/resource1.jar");
URL url = file.toURI().toURL();
URL[] urls = new URL[]{url};
ClassLoader cl = new URLClassLoader(urls, Main.class.getClassLoader());
ResourceConfig rc = new ResourceConfig()
.setClassLoader(cl)
.files("/path/resource1.jar");
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(
BASE_URI, rc);
可能有更优雅的方式...
关于java - 使用 Grizzly 作为 Web 服务引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21381305/