gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 20:16:55 28 4
gpt4 key购买 nike

使用 Jersey 编写 Web 服务时,我将 ServletContainer 添加到 web.xml

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

这个ServletContainer有什么用呢? tomcat不是ServletContainer吗?
有没有办法在没有 Jersey 或任何其他第三方框架的情况下实现 Rest Web 服务?

最佳答案

What is the use of this ServletContainer?

通常任何类的使用都会在其 javadoc 中进行解释。幸运的是, Jersey 的人们认真对待 javadocs,你的答案确实是 right there .

com.sun.jersey.spi.container.servlet

Class ServletContainer

A Servlet or Filter for deploying root resource classes.

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.

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. 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. 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).

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. The initialization parameter "com.sun.jersey.config.property.packages" MUST be set to provide one or more package names. 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". Any additional initialization parameters are then added to the Map instance. Then that Map instance is passed to the constructor of PackagesResourceConfig.

If none of the above resource configuration related initialization parameters are present a new instance of WebAppResourceConfig is created. The initialization parameter "com.sun.jersey.config.property.classpath" MAY be set to provide one or more resource paths. 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". Any additional initialization parameters are then added to the Map instance. Then that Map instance is passed to the constructor of WebAppResourceConfig. If the initialization parameter is not present then the following resource paths are utilized: "/WEB-INF/lib" and "/WEB-INF/classes".

All initialization parameters are added as properties of the created 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. If this class is used as a Servlet then the ServletConfig class may be injected. If this class is used as a Filter then the FilterConfig class may be injected. WebConfig may be injected to abstract servlet or filter deployment.

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.

<小时/>

isn't tomcat the ServletContainer?

是的,确实如此。只是类的名称 com.sun.jersey.spi.container.servlet.ServletContainer 选得不好。他们最好将其命名为 RestControllerRestApiDispatcher 左右。

<小时/>

Is there a way to implement Rest web services without Jersey or any other third party framework?

是的。您有多种选择:

  • Homegrow your own framework 。在业余爱好项目中作为学习练习很有趣,但在生产应用程序中这样做时,从长远来看,它只会给你带来麻烦。很难。
  • 或者更好,replace Tomcat by a real Java EE server 。它内置了 JAX-RS 等功能,您可以随时使用,无需太多手动配置麻烦。根据 Java EE 服务器,JAX-RS 实现可能依次由 Jersey 表示。 (GlassFish/Payara)或RESTEasy (WildFly/JBossAS)或CXF (TomEE)或其他东西。

关于java - 使用 Jersey 编写 REST Web 服务时使用 ServletContainer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45595278/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com