Tomcat是否开箱即用地支持JAX-RS(它可以识别JAX-RS)吗? [英] Does Tomcat support JAX-RS out of the box (is it JAX-RS aware)?

查看:115
本文介绍了Tomcat是否开箱即用地支持JAX-RS(它可以识别JAX-RS)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如果我们的应用服务器支持JAX-RS,或者与JAX-RS紧密集成,则将我们的 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> 

如果我们的应用服务器不支持JAX-RS,则必须指定处理JAX-RS调用的JAX-RS提供程序的servlet.应将Application类指定为servlet的初始化参数:

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

解决方案

"Tomcat是JAX-RS感知的Servlet容器吗?"

否.

<您如何区分一个不知道JAX-RS的servlet容器JAX-RS?"

这仅仅是一个Servlet容器,应告诉您它不是 "JAX-RS感知的".JAX-RS是Java EE规范的一部分.Servlet容器完全支持其名称所隐含的含义.Servlet的容器​​.他们可能支持其他一些小功能,例如JSP,但不支持整个EE规范.这不是他们设计的一部分.如果要在Servlet容器中使用JAX-RS,则需要添加一个实现,例如 Jersey Resteasy

当您说Servlet容器时,您会想到诸如Jetty,Tomcat,Undertow和Grizzly之类的服务器.如果要全面支持Java EE,则需要获得一个支持整个规范的实际Java EE应用程序服务器,例如JBoss/Wildfly,Glassfish,TomEE,WebSphere,WebLogic.

为什么在第一种情况下可以将自定义类扩展到javax.ws.rs.core.Application作为Servlet?"

我无法使用Glassfish 4.0或Wildfly 8.1生成有效的示例,也未在 JAX-RS规范-润滑-2.3.2 Servlet 以获得有关标准JAX-RS部署选项的完整规范.未指定的任何其他部署/配置选项都是特定于实现的.

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?

解决方案

"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 the ContainerInitializer MUST be named with the fully qualified name of the Application subclass.

这篇关于Tomcat是否开箱即用地支持JAX-RS(它可以识别JAX-RS)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆