简体   繁体   English

使用Jersey编写REST Web服务时使用ServletContainer

[英]using ServletContainer when writing REST Web Service using Jersey

When writing web services using Jersey I am adding a ServletContainer to the web.xml 使用Jersey编写Web服务时,我正在向Web.xml中添加一个ServletContainer。

 <servlet>
    <servlet-name>Back-End API</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

What is the use of this ServletContainer? 这个ServletContainer有什么用? isn't tomcat the ServletContainer? 是不是tomcat ServletContainer?
Is there a way to implement Rest web services without Jersey or any other third party framework? 没有Jersey或任何其他第三方框架,有没有办法实现Rest Web服务?

What is the use of this ServletContainer? 这个ServletContainer有什么用?

Usually the use of any class is explained in its javadoc. 通常,任何类的用法都在其javadoc中进行了说明。 Fortunately the Jersey guys took javadocs seriously and your answer is indeed right there . 幸运的是,泽西岛的人们认真对待了Javadocs,您的答案确实在那里

com.sun.jersey.spi.container.servlet com.sun.jersey.spi.container.servlet

Class ServletContainer ServletContainer类

A Servlet or Filter for deploying root resource classes. 用于部署根资源类的ServletFilter

If this class is declared as a filter and the initialization parameter PROPERTY_WEB_PAGE_CONTENT_REGEX is not set or FEATURE_FILTER_FORWARD_ON_404 is not set to true then the filter must be declared at the last position in the filter chain as the filter will not forward any request to a next filter (if any) in the chain. 如果将此类声明为过滤器,并且未设置初始化参数PROPERTY_WEB_PAGE_CONTENT_REGEXFEATURE_FILTER_FORWARD_ON_404未设置为true,则必须在过滤器链的最后位置声明该过滤器,因为该过滤器不会将任何请求转发到下一个过滤器(如果有的话)。

The following sections make reference to initialization parameters. 以下各节参考初始化参数。 Unless otherwise specified the initialization parameters apply to both server and filter initialization parameters. 除非另有说明,否则初始化参数适用于服务器和过滤器初始化参数。

The servlet or filter may be configured to have an initialization parameter "com.sun.jersey.config.property.resourceConfigClass" or "javax.ws.rs.Application" and whose value is a fully qualified name of a class that implements ResourceConfig or Application . 可以将Servlet或过滤器配置为具有初始化参数“ com.sun.jersey.config.property.resourceConfigClass”或“ javax.ws.rs.Application”,并且其值是实现ResourceConfig或的类的完全限定名称。 Application If the concrete class has a constructor that takes a single parameter of the type Map then the class is instantiated with that constructor and an instance of Map that contains all the initialization parameters is passed as the parameter. 如果具体类具有一个采用Map类型的单个参数的构造函数,则该类将使用该构造函数实例化,并且包含所有初始化参数的Map实例将作为参数传递。 Otherwise, the class is instantiated as a singleton component managed by the runtime, and injection may be performed (the artifacts that may be injected are limited to injectable providers registered when the servlet or filter is configured). 否则,将该类实例化为由运行时管理的单例组件,并且可以执行注入(可以注入的工件仅限于在配置Servlet或过滤器时注册的可注入提供程序)。

If the initialization parameter "com.sun.jersey.config.property.resourceConfigClass" or "javax.ws.rs.Application" is not present and a initialization parameter "com.sun.jersey.config.property.packages" is present (see PackagesResourceConfig.PROPERTY_PACKAGES ) a new instance of PackagesResourceConfig is created. 如果不存在初始化参数“ com.sun.jersey.config.property.resourceConfigClass”或“ javax.ws.rs.Application”,并且存在初始化参数“ com.sun.jersey.config.property.packages”(见PackagesResourceConfig.PROPERTY_PACKAGES )的新实例PackagesResourceConfig创建。 The initialization parameter "com.sun.jersey.config.property.packages" MUST be set to provide one or more package names. 必须将初始化参数“ com.sun.jersey.config.property.packages”设置为提供一个或多个软件包名称。 Each package name MUST be separated by ';'. 每个软件包名称必须用';'分隔。 The package names are added as a property value to a Map instance using the property name " com.sun.jersey.config.property.packages ". 使用属性名称“ com.sun.jersey.config.property.packages ”将包名称作为属性值添加到Map实例。 Any additional initialization parameters are then added to the Map instance. 然后,将任何其他初始化参数添加到Map实例。 Then that Map instance is passed to the constructor of PackagesResourceConfig . 然后,将该Map实例传递给PackagesResourceConfig的构造函数。

If none of the above resource configuration related initialization parameters are present a new instance of WebAppResourceConfig is created. 如果以上资源配置相关的初始化参数均不存在, WebAppResourceConfig创建WebAppResourceConfig的新实例。 The initialization parameter "com.sun.jersey.config.property.classpath" MAY be set to provide one or more resource paths. 可以将初始化参数“ com.sun.jersey.config.property.classpath”设置为提供一个或多个资源路径。 Each path MUST be separated by ';'. 每个路径必须以';'分隔。 The resource paths are added as a property value to a Map instance using the property name " com.sun.jersey.config.property.classpath ". 使用属性名称“ com.sun.jersey.config.property.classpath ”将资源路径作为属性值添加到Map实例。 Any additional initialization parameters are then added to the Map instance. 然后,将任何其他初始化参数添加到Map实例。 Then that Map instance is passed to the constructor of WebAppResourceConfig . 然后,将该Map实例传递给WebAppResourceConfig的构造WebAppResourceConfig If the initialization parameter is not present then the following resource paths are utilized: "/WEB-INF/lib" and "/WEB-INF/classes". 如果不存在初始化参数,则使用以下资源路径:“ / WEB-INF / lib”和“ / WEB-INF / classes”。

All initialization parameters are added as properties of the created ResourceConfig . 所有初始化参数都作为创建的ResourceConfig属性添加。

A new WebApplication instance will be created and configured such that the following classes may be injected onto a root resource, provider and Application classes using Context: HttpServletRequest , HttpServletResponse , ServletContext , ServletConfig and WebConfig . 将创建并配置一个新的WebApplication实例,以便可以使用Context将以下类注入到根资源,提供者和Application类中: HttpServletRequestHttpServletResponseServletContextServletConfigWebConfig If this class is used as a Servlet then the ServletConfig class may be injected. 如果将此类用作Servlet则可以注入ServletConfig类。 If this class is used as a Filter then the FilterConfig class may be injected. 如果将此类用作Filter则可以注入FilterConfig类。 WebConfig may be injected to abstract servlet or filter deployment. 可以注入WebConfig来抽象servlet或过滤器部署。

A IoCComponentProviderFactory instance may be registered by extending this class and overriding the method initiate(ResourceConfig, WebApplication) to initiate the WebApplication with the IoCComponentProviderFactory instance. IoCComponentProviderFactory实例可以通过扩展此类并重写方法IoCComponentProviderFactory initiate(ResourceConfig, WebApplication)来使用IoCComponentProviderFactory实例启动WebApplicationIoCComponentProviderFactory


isn't tomcat the ServletContainer? 是不是tomcat ServletContainer?

Yup, it is. 是的,是的。 It's just that the name of the class com.sun.jersey.spi.container.servlet.ServletContainer is badly chosen. 仅仅是错误地选择了类com.sun.jersey.spi.container.servlet.ServletContainer的名称。 They'd better have named it RestController or RestApiDispatcher orso. 他们最好将其命名为RestControllerRestApiDispatcher orso。


Is there a way to implement Rest web services without Jersey or any other third party framework? 没有Jersey或任何其他第三方框架,有没有办法实现Rest Web服务?

Yup. 对。 You have several options: 您有几种选择:

  • Homegrow your own framework . 自行开发自己的框架 Fun as learning exercise in a hobby project, but when doing so for a production application, it will only bite you in long term. 在爱好项目中作为学习练习很有趣,但是对于生产应用程序来说,这样做只会长期困扰您。 Hard. 硬。
  • Or, better, replace Tomcat by a real Java EE server . 或者,最好用真正的Java EE服务器代替Tomcat It has among others JAX-RS built-in and you're ready to go without much manual configuration hassle. 它具有内置的JAX-RS等,您无需太多的手动配置麻烦就可以准备就绪。 Depending on the Java EE server, the JAX-RS implementation may in turn be represented by Jersey (GlassFish/Payara) or RESTEasy (WildFly/JBossAS) or CXF (TomEE) or something else. 取决于Java EE服务器,JAX-RS实现可以依次由Jersey (GlassFish / Payara)或RESTEasy (WildFly / JBossAS)或CXF (TomEE)表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM