JavaSheriff
JavaSheriff

Reputation: 7677

using ServletContainer when writing REST Web Service using Jersey

When writing web services using Jersey I am adding a ServletContainer to the web.xml

 <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? isn't tomcat the ServletContainer?
Is there a way to implement Rest web services without Jersey or any other third party framework?

Upvotes: 0

Views: 1086

Answers (1)

BalusC
BalusC

Reputation: 1109072

What is the use of this ServletContainer?

Usually the use of any class is explained in its javadoc. Fortunately the Jersey guys took javadocs seriously and your answer is indeed 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?

Yup, it is. It's just that the name of the class com.sun.jersey.spi.container.servlet.ServletContainer is badly chosen. They'd better have named it RestController or RestApiDispatcher orso.


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

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. It has among others JAX-RS built-in and you're ready to go without much manual configuration hassle. 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.

Upvotes: 3

Related Questions