12

I can start grizzly and deploy Jersey webservices on it with the following lines.

protected HttpServer create() throws Throwable {
  ResourceConfig rc = new PackagesResourceConfig("com.resource", "com.provider");
  HttpServer server = GrizzlyServerFactory.createHttpServer(uri, rc);
  return server;
}

But is there a way to load a web.xml instead of a ResourceConfig?

<web-app>
  <servlet>
    <servlet-name>Jersey</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.resource, com.provider</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>Jersey</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>
Oliver
  • 3,815
  • 8
  • 35
  • 63
yves amsellem
  • 7,106
  • 5
  • 44
  • 68

1 Answers1

0

It seems that there is currently no direct way to configure grizzly with a web.xml. However I have used a partial solution that may be a beginning.

web.xml

First to understand the solution, we must understand what is the meaning of using a web.xml. It is basically use for configure your web application (see this answer for a more detail). In this case we are configuring init-params for the servlet.

The (partial) solution

Instead of using the web.xml and instead of using ResouceConfig.class, we can use Grizzly as our servlet and initializing the parameters. For example

<web-app>
  <servlet>
    <servlet-name>Jersey</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.resource, com.provider</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>Jersey</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

would give something like :

protected HttpServer create() throws Throwable {
    HashMap<String, String> initParams = new HashMap<>();

    //ServerProperties.PROVIDER_PACKAGES is equal to "jersey.config.server.provider.packages"
    initParams.put(ServerProperties.PROVIDER_PACKAGES, "com.resource,com.provider");

    //Make sure to end the URI with a forward slash
    HttpServer server = GrizzlyWebContainerFactory.create("http://localhost:8080/", initParams);
    return server;
}

With this, we can therefore put all the init-params that we want to. However this solution cannot replace a whole web.xml.

Community
  • 1
  • 1
DragonCai
  • 84
  • 5