Glassfish 3.0.1 上的 Jersey JAX-RS:我的 web.xml 中可以有一个空的 webapp 元素吗?
我的理解是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于符合 Java EE 6 的 JAX-RS 实现,我认为您需要执行以下操作:
javax.ws.rs.core.Application
子类@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:
javax.ws.rs.core.Application
sub-class to your web project@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 yourApplication.getClasses()
.仅当您在 web.xml 中声明正确的版本时,可能才支持处理 web-fragment.xml 文件。这样,当部署到新版本的应用程序服务器时,现有应用程序的行为不会突然改变。
请尝试使用这样的 web.xml,该文件是从 Netbeans 中新创建的 ee6 Web 项目复制的。
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.