Grizzly JAX-WS和JAX-RS

时间:2016-01-11 12:14:59

标签: jax-rs jax-ws grizzly

我使用灰熊创建了两个项目。

一个用于jax-rs,一个用于jax-ws。

运行jax-rs的代码如下所示:

    String BASE_URI = "http://localhost:8080/myapp/";
    ResourceConfig rc = new ResourceConfig().packages("za.co.quinn.grizzly.rest");
    HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);

运行jax-ws的代码如下所示:

    HttpServer httpServer = new HttpServer();
    ServerConfiguration configuration = httpServer.getServerConfiguration();
    configuration.addHttpHandler(new JaxwsHandler(new AddService()), "/add");
    httpServer.addListener(new NetworkListener("jaxws-listener", "0.0.0.0", 8080));
    httpServer.start();

我想将两者结合起来让jax-ws和jax-rs在同一个项目中工作。

拥有一个JaxrsHandler会很好,我可以这样添加:

configuration.addHttpHandler(new JaxrsHandler(new AddAnotherService()), "/addAnother");

但是没有JaxrsHandler存在。

我可以采用另一种方式将两者结合起来吗?

1 个答案:

答案 0 :(得分:1)

这解决了我的问题:

    Injector injector = Guice.createInjector(new JpaPersistModule("myJpaUnit"),
            new ServletModule() {
                @Override
                protected void configureServlets() {
                    bind(new TypeLiteral<ExerciseDao>() {
                    }).to(ExerciseDaoImpl.class);
                }
            });

    ResourceConfig rc = new PackagesResourceConfig("za.co.quinn.ws");
    IoCComponentProviderFactory ioc = new GuiceComponentProviderFactory(rc, injector);

    PersistInitializer initializer = injector.getInstance(PersistInitializer.class);
    HttpServer server = GrizzlyServerFactory.createHttpServer(BASE_URI, rc, ioc);