与书中所写内容最接近的是将应用程序类的名称指定为< servlet-name> (这是 的一部分),但完全不依赖于部署到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> 这是来自JAX-RS规范阅读 JAX-RS规范-润滑-2.3.2 Servlet 以获得有关标准JAX-RS部署选项的完整规范.未指定的任何其他部署/配置选项都是特定于实现的.From the textbook "RESTful Java with JAX-RS" we can read:<?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>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? 解决方案 No.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 ResteasyWhen 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.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 specRead 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. 这篇关于Tomcat是否开箱即用地支持JAX-RS(它可以识别JAX-RS)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-29 10:49
查看更多