使用 web.xml 配置 Grizzly

发布于 2024-12-28 18:16:34 字数 1047 浏览 1 评论 0原文

我可以使用以下几行启动 grizzly 并在其上部署 Jersey Web 服务。

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

但是有没有办法加载 web.xml 而不是 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城空念 2025-01-04 18:16:34

目前似乎没有直接的方法可以使用 web.xml 配置 grizzly。不过,我使用了部分解决方案,这可能是一个开始。

web.xml

首先要了解解决方案,我们必须了解使用web.xml的含义是什么。它基本上用于配置您的 Web 应用程序(有关更多详细信息,请参阅此答案)。在本例中,我们正在为 servlet 配置 init-params

(部分)解决方案

我们可以使用 Grizzly 作为 servlet 并初始化参数,而不是使用 web.xmlResouceConfig.class
例如,

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

会给出如下内容:

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;
}

有了这个,我们就可以放置我们想要的所有 init-params
但是,此解决方案无法替换整个 web.xml

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文