Glassfish 3.0.1 上的 Jersey JAX-RS:我的 web.xml 中可以有一个空的 webapp 元素吗?

发布于 2024-09-06 03:30:44 字数 791 浏览 8 评论 0原文

我的理解是 glassfish 3 符合 JEE6 且支持 jax-rs,因此我不必在 web.xml 文件中包含 jersey servlet 容器和映射。这就是我希望我的 web.xml 看起来像

<webapp>
</webapp>

这样的,但这不起作用,因为当我尝试访问我的 jax-rs 路径注释资源时,我收到 404。当我包含 servlet 适配器时,效果很好,如下所示:

<webapp>
  <servlet>
    <servlet-name>ServletAdaptor</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servle
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletAdaptor</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</webapp>

Is it possible to have an empty webapp element using jersey on glassfish 3.0.1?

My understanding is that glassfish 3 is JEE6 compliant and jax-rs-aware, and that consequently I do not have to include the jersey servlet container and mapping in my web.xml file. Here's what I was hoping my web.xml could look like

<webapp>
</webapp>

That doesn't work though, as I get 404's when I try to hit my jax-rs path-anotated resources. It works great when I include the servlet adaptor, like this:

<webapp>
  <servlet>
    <servlet-name>ServletAdaptor</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servle
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletAdaptor</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</webapp>

Is it possible to have an empty webapp element using jersey on glassfish 3.0.1?

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

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

发布评论

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

评论(2

空名 2024-09-13 03:30:44

对于符合 Java EE 6 的 JAX-RS 实现,我认为您需要执行以下操作:

  1. 向您的 Web 项目添加一个 javax.ws.rs.core.Application 子类
  2. 然后,添加一个 @javax.ws.rs.ApplicationPath("/*") Application 子类的注释。

您的应用程序子类不必执行任何操作。根据规范,这意味着应用程序中找到的任何 @Path/@Provider 都将包含在应用程序中,然后您的 @ApplicationPath 就像您的 servlet 映射。这将使您拥有一个空的 web.xml。

没有 web.xml 的问题是 JAX-RS 实现将不知道要使用哪个 servlet 映射(url 模式)。此外,如果您希望仅返回 @Path/@Provider 注释类的子集,则这允许您仅将某些资源/提供程序可用于特定的 URL 模式。 Application.getClasses()

For Java EE 6 compliant JAX-RS implementations, I think you need to do the following:

  1. Add a javax.ws.rs.core.Application sub-class to your web project
  2. Then, add a @javax.ws.rs.ApplicationPath("/*") annotation to the Application sub-class.

Your Application sub-class shouldn't have to do anything. By the spec this implies that any @Path/@Provider found in the app will be included in the application and then your @ApplicationPath is like your servlet mapping. This will let you have an empty web.xml.

The issue with having no web.xml is that a JAX-RS implementation will not know which servlet mapping (url pattern) to use. Also, this allows you to have only certain resources/providers available for a particular URL pattern if you wish to return only a subset of @Path/@Provider annotated classes in your Application.getClasses().

青春有你 2024-09-13 03:30:44

仅当您在 web.xml 中声明正确的版本时,可能才支持处理 web-fragment.xml 文件。这样,当部署到新版本的应用程序服务器时,现有应用程序的行为不会突然改变。

请尝试使用这样的 web.xml,该文件是从 Netbeans 中新创建的 ee6 Web 项目复制的。

<web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
</web-app>

Processing of web-fragment.xml files is probably only supported when you declare the correct version in your web.xml. This way the behaviour of existing applications don't suddenly change when deployed to a new version of an application server.

Please try it with a web.xml like this, which is copied from a newly created ee6 web project in Netbeans.

<web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
</web-app>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文