[英]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
orFilter
for deploying root resource classes.用于部署根资源类的
Servlet
或Filter
。If this class is declared as a filter and the initialization parameter
PROPERTY_WEB_PAGE_CONTENT_REGEX
is not set orFEATURE_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_REGEX
或FEATURE_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
orApplication
.可以将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 ofPackagesResourceConfig
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
andWebConfig
.将创建并配置一个新的
WebApplication
实例,以便可以使用Context将以下类注入到根资源,提供者和Application类中:HttpServletRequest
,HttpServletResponse
,ServletContext
,ServletConfig
和WebConfig
。 If this class is used as aServlet
then theServletConfig
class may be injected.如果将此类用作
Servlet
则可以注入ServletConfig
类。 If this class is used as aFilter
then theFilterConfig
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 methodinitiate(ResourceConfig, WebApplication)
to initiate theWebApplication
with theIoCComponentProviderFactory
instance.IoCComponentProviderFactory
实例可以通过扩展此类并重写方法IoCComponentProviderFactory
initiate(ResourceConfig, WebApplication)
来使用IoCComponentProviderFactory
实例启动WebApplication
来IoCComponentProviderFactory
。
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. 他们最好将其命名为
RestController
或RestApiDispatcher
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:
您有几种选择:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.