Java Reference
In-Depth Information
package
package
com
.
restfully
.
shop
.
services
;
import
import
javax.ws.rs.ApplicationPath
javax.ws.rs.ApplicationPath
;
import
import
javax.ws.rs.core.Application
javax.ws.rs.core.Application
;
import
import
java.util.HashSet
java.util.HashSet
;
import
import
java.util.Set
java.util.Set
;
@ApplicationPath
(
"/services"
)
public
public class
class
ShoppingApplication
ShoppingApplication
extends
extends
Application
{
private
private
Set
<
Object
>
singletons
=
new
new
HashSet
<
Object
>();
private
private
Set
<
Class
<?>>
empty
=
new
new
HashSet
<
Class
<?>>();
public
public
ShoppingApplication
() {
singletons
.
add
(
new
new
CustomerResource
());
}
@Override
public
public
Set
<
Class
<?>>
getClasses
() {
return
return
empty
;
}
@Override
public
public
Set
<
Object
>
getSingletons
() {
return
return
singletons
;
}
}
The
@ApplicationPath
defines the relative base URL path for all our JAX-RS services in
the deployment. So, in this example, all of our JAX-RS RESTful services will be prefixed
with the
/services
path when we execute on them. For our customer service database ex-
ample, we do not have any per-request services, so our
ShoppingApplica-
tion.getClasses()
method returns an empty set. Our
ShoppingApplica-
tion.getSingletons()
method returns the
Set
we initialized in the constructor. This
Set
contains an instance of
CustomerResource
.
In Java EE and standalone servlet deployments, JAX-RS classes must be deployed within the
application server's servlet container as a Web ARchive (WAR). Think of a servlet container
as your application server's web server. A WAR is a JAR file that, in addition to Java class
files, also contains other Java libraries along with dynamic (like JSPs) and static content (like
HTML files or images) that you want to publish on your website. We need to place our Java
classes within this archive so that our application server can deploy them. Here's what the
structure of a WAR file looks like: