Tomcat是一个支持JAX-RS的Servlet容器

时间:2022-03-15 19:34:00

From the textbook "RESTful Java with JAX-RS" we can read:

从教科书“RESTful Java with JAX-RS”我们可以阅读:

If our application server is JAX-RS-aware or, in other words, is tightly integrated with JAX-R declare our ShoppingApplication class as a servlet:

如果我们的应用程序服务器支持JAX-RS,或者换句话说,它与JAX-R紧密集成,则将我的ShoppingApplication类声明为servlet:

<?xml version="1.0"?>
 <web-app>
 <servlet>
 <servlet-name>Rest</servlet-name>
 <servlet-class>
 com.restfully.shop.services.ShoppingApplication
 </servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>Rest</servlet-name>
 <url-pattern>/*</url-pattern>
 </servlet-mapping>
 </web-app> 

If our application server is not JAX-RS-aware, you will have to specify the JAX-RS provider’s servlet that handles JAX-RS invocations. The Application class should be specified as an init-param of the servlet:

如果我们的应用程序服务器不支持JAX-RS,则必须指定处理JAX-RS调用的JAX-RS提供程序的servlet。应该将Application类指定为servlet的init-param:

Now my question is: Is Tomcat a JAX-RS aware Servlet container? How do you distinguish a servlet container JAX-RS aware from one wich is not JAX-RS aware? Why in the first case it's possible to use your custom class wich extends javax.ws.rs.core.Application as a Servlet?

现在我的问题是:Tomcat是一个支持JAX-RS的Servlet容器吗?你如何区分一个不知道JAX-RS的servlet容器JAX-RS?为什么在第一种情况下可以使用自定义类将javax.ws.rs.core.Application扩展为Servlet?

1 个解决方案

#1


"Is Tomcat a JAX-RS aware Servlet container?"

“Tomcat是一个支持JAX-RS的Servlet容器吗?”

No.

"How do you distinguish a servlet container JAX-RS aware from one wich is not JAX-RS aware?"

“你如何区分一个不知道JAX-RS的servlet容器JAX-RS?”

The fact this it is only a Servlet container, should tell you that it is not "JAX-RS aware". JAX-RS is part of the Java EE specification. Servlet containers supports exactly what their name implies; a container for Servlets. They might have support for other little features like JSP, but will not support the entire EE spec. This is not part of their design. If you want to use JAX-RS in a Servlet container, you need to add an implementation, like Jersey or Resteasy

事实上,它只是一个Servlet容器,应该告诉你它不是“JAX-RS识别”。 JAX-RS是Java EE规范的一部分。 Servlet容器支持其名称所暗示的内容; Servlets的容器。他们可能支持JSP等其他一些小功能,但不支持整个EE规范。这不是他们设计的一部分。如果要在Servlet容器中使用JAX-RS,则需要添加一个实现,如Jersey或Resteasy

When you say Servlet container you think of servers like Jetty, Tomcat, Undertow, Grizzly. If you want full Java EE support then you need to get an actual Java EE application server that supports the entire spec, like JBoss/Wildfly, Glassfish, TomEE, WebSphere, WebLogic.

当你说Servlet容器时,你会想到像Jetty,Tomcat,Undertow,Grizzly这样的服务器。如果您需要完整的Java EE支持,那么您需要获得支持整个规范的实际Java EE应用程序服务器,例如JBoss / Wildfly,Glassfish,TomEE,WebSphere,WebLogic。

"Why in the first case it's possible to use your custom class wich extends javax.ws.rs.core.Application as a Servlet?"

“为什么在第一种情况下可以使用你的自定义类将javax.ws.rs.core.Application扩展为Servlet?”

tl;dr

If by "tightly integrated with JAX-RS", the author means a Server is by default compliant with the JAX-RS spec (pretty much a Java EE application sever), then the following statement (along with the corresponding web.xml configuration) is completely false:

如果通过“与JAX-RS紧密集成”,则作者意味着服务器默认符合JAX-RS规范(几乎是Java EE应用程序服务器),然后是以下语句(以及相应的web.xml配置)是完全错误的:

"If our application server is JAX-RS-aware or, in other words, is tightly integrated with JAX-RS declare our ShoppingApplication class as a servlet:"

“如果我们的应用服务器支持JAX-RS,或者换句话说,它与JAX-RS紧密集成,则将我的ShoppingApplication类声明为servlet:”


I was not able to produce a working example with either Glassfish 4.0 or Wildfly 8.1, nor is this specified anywhere in the JAX-RS specification. In Glassfish, I'll get an exception about ShoppingApplication not being a Servlet, and in Wildfly I'll just get a NotFoundException, meaning the application is never loaded.

我无法使用Glassfish 4.0或Wildfly 8.1生成一个工作示例,也未在JAX-RS规范中的任何位置指定。在Glassfish中,我会得到一个关于ShoppingApplication不是Servlet的例外,而在Wildfly中我只会得到一个NotFoundException,这意味着永远不会加载应用程序。

The closest thing I could find to what the book states, is to specify the name of the application class as the <servlet-name> (which is part of the JAX-RS spec, but is not at all dependent on being deployed to a Java EE server)

我能找到的最接近的内容是将应用程序类的名称指定为 (它是JAX-RS规范的一部分,但完全不依赖于部署到Java EE服务器)

<servlet>
    <servlet-name>com.restfully.shop.services.ShoppingApplication</servlet-name>
</servlet>
<servlet-mapping>
    <servlet-name>com.restfully.shop.services.ShoppingApplication</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

This is from the JAX-RS spec

这来自JAX-RS规范

If an Application subclass is present that is not being handled by an existing servlet then the servlet added by the ContainerInitializer MUST be named with the fully qualified name of the Application subclass.

如果存在未由现有servlet处理的Application子类,则ContainerInitializer添加的servlet必须使用Application子类的完全限定名称命名。

  • Read JAX-RS spec - Plublication - 2.3.2 Servlet for the completely specification on standard JAX-RS deployment options. Any other deployment/configuration option not specified is implementation specific.
  • 阅读JAX-RS规范 - Plublication - 2.3.2 Servlet,用于标准JAX-RS部署选项的完整规范。未指定的任何其他部署/配置选项都是特定于实现的。

#1


"Is Tomcat a JAX-RS aware Servlet container?"

“Tomcat是一个支持JAX-RS的Servlet容器吗?”

No.

"How do you distinguish a servlet container JAX-RS aware from one wich is not JAX-RS aware?"

“你如何区分一个不知道JAX-RS的servlet容器JAX-RS?”

The fact this it is only a Servlet container, should tell you that it is not "JAX-RS aware". JAX-RS is part of the Java EE specification. Servlet containers supports exactly what their name implies; a container for Servlets. They might have support for other little features like JSP, but will not support the entire EE spec. This is not part of their design. If you want to use JAX-RS in a Servlet container, you need to add an implementation, like Jersey or Resteasy

事实上,它只是一个Servlet容器,应该告诉你它不是“JAX-RS识别”。 JAX-RS是Java EE规范的一部分。 Servlet容器支持其名称所暗示的内容; Servlets的容器。他们可能支持JSP等其他一些小功能,但不支持整个EE规范。这不是他们设计的一部分。如果要在Servlet容器中使用JAX-RS,则需要添加一个实现,如Jersey或Resteasy

When you say Servlet container you think of servers like Jetty, Tomcat, Undertow, Grizzly. If you want full Java EE support then you need to get an actual Java EE application server that supports the entire spec, like JBoss/Wildfly, Glassfish, TomEE, WebSphere, WebLogic.

当你说Servlet容器时,你会想到像Jetty,Tomcat,Undertow,Grizzly这样的服务器。如果您需要完整的Java EE支持,那么您需要获得支持整个规范的实际Java EE应用程序服务器,例如JBoss / Wildfly,Glassfish,TomEE,WebSphere,WebLogic。

"Why in the first case it's possible to use your custom class wich extends javax.ws.rs.core.Application as a Servlet?"

“为什么在第一种情况下可以使用你的自定义类将javax.ws.rs.core.Application扩展为Servlet?”

tl;dr

If by "tightly integrated with JAX-RS", the author means a Server is by default compliant with the JAX-RS spec (pretty much a Java EE application sever), then the following statement (along with the corresponding web.xml configuration) is completely false:

如果通过“与JAX-RS紧密集成”,则作者意味着服务器默认符合JAX-RS规范(几乎是Java EE应用程序服务器),然后是以下语句(以及相应的web.xml配置)是完全错误的:

"If our application server is JAX-RS-aware or, in other words, is tightly integrated with JAX-RS declare our ShoppingApplication class as a servlet:"

“如果我们的应用服务器支持JAX-RS,或者换句话说,它与JAX-RS紧密集成,则将我的ShoppingApplication类声明为servlet:”


I was not able to produce a working example with either Glassfish 4.0 or Wildfly 8.1, nor is this specified anywhere in the JAX-RS specification. In Glassfish, I'll get an exception about ShoppingApplication not being a Servlet, and in Wildfly I'll just get a NotFoundException, meaning the application is never loaded.

我无法使用Glassfish 4.0或Wildfly 8.1生成一个工作示例,也未在JAX-RS规范中的任何位置指定。在Glassfish中,我会得到一个关于ShoppingApplication不是Servlet的例外,而在Wildfly中我只会得到一个NotFoundException,这意味着永远不会加载应用程序。

The closest thing I could find to what the book states, is to specify the name of the application class as the <servlet-name> (which is part of the JAX-RS spec, but is not at all dependent on being deployed to a Java EE server)

我能找到的最接近的内容是将应用程序类的名称指定为 (它是JAX-RS规范的一部分,但完全不依赖于部署到Java EE服务器)

<servlet>
    <servlet-name>com.restfully.shop.services.ShoppingApplication</servlet-name>
</servlet>
<servlet-mapping>
    <servlet-name>com.restfully.shop.services.ShoppingApplication</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

This is from the JAX-RS spec

这来自JAX-RS规范

If an Application subclass is present that is not being handled by an existing servlet then the servlet added by the ContainerInitializer MUST be named with the fully qualified name of the Application subclass.

如果存在未由现有servlet处理的Application子类,则ContainerInitializer添加的servlet必须使用Application子类的完全限定名称命名。

  • Read JAX-RS spec - Plublication - 2.3.2 Servlet for the completely specification on standard JAX-RS deployment options. Any other deployment/configuration option not specified is implementation specific.
  • 阅读JAX-RS规范 - Plublication - 2.3.2 Servlet,用于标准JAX-RS部署选项的完整规范。未指定的任何其他部署/配置选项都是特定于实现的。