问题
From the textbook "RESTful Java with JAX-RS" we can read:
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:
<?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:
Now my question is: Is Tomcat a JAX-RS aware Servlet container? How do you distinguish a servlet container JAX-RS aware from one which is not JAX-RS aware? Why in the first case it's possible to use your custom class which extends javax.ws.rs.core.Application
as a Servlet?
回答1:
"Is Tomcat a JAX-RS aware Servlet container?"
No.
"How do you distinguish a servlet container JAX-RS aware from one wich is not JAX-RS aware?"
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
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.
"Why in the first case it's possible to use your custom class wich extends javax.ws.rs.core.Application as a 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.
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)
<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
If an
Application
subclass is present that is not being handled by an existing servlet, then the servlet added by theContainerInitializer
MUST be named with the fully qualified name of theApplication
subclass.
- 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.
来源:https://stackoverflow.com/questions/30176555/does-tomcat-support-jax-rs-out-of-the-box-is-it-jax-rs-aware