Publishing Jersey service instance to Grizzly

元气小坏坏 提交于 2019-12-11 17:34:20

问题


I can publish a jersey service to grizzly by doing the following

final String baseUri = "http://localhost:51000";
final Map<String, String> initParams = new HashMap<String, String>();
initParams.put("com.sun.jersey.config.property.packages", "my.rest.service");
SelectorThread threadSelector = GrizzlyWebContainerFactory.create(baseUri, initParams);

So the specified package will be scanned for any service classes annotated with @Path, and they will be initialized. My question is, is there any way to specify a pre-initialized jersey service class - initialied via a custom factory or even just by doing new MyService() - and publish it to Grizzly or any other container?


回答1:


You can use GrizzlyServerFactory.create(URI u, ResourceConfig rc) and add your singletons to provided ResourceConfig. ie:

final String baseUri = "http://localhost:51000";
final ResourceConfig rc = new PackagesResourceConfig("my.rest.service");
rc.getSingletons().add(new MyInstantiatedResource());
SelectorThread threadSelector = GrizzlyServerFactory.create(baseUri, rc);



回答2:


or any other container?

Jersey also supports the SimpleHttp container, if you add the add-on jersey module and use SimpleServerFactory.

ResourceConfig config = new DefaultResourceConfig();
config.getSingletons().add(new MyInstantiatedResource());

String serverBase = "http://localhost:8080/foo";
Closeable server = SimpleServerFactory.create(serverBase, config);


来源:https://stackoverflow.com/questions/9306198/publishing-jersey-service-instance-to-grizzly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!