问题
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