Java Reference
In-Depth Information
Let's look at a simple example of an
Application
class:
package
package
com
.
restfully
.
shop
.
services
;
import
import
javax.ws.rs.core.Application
javax.ws.rs.core.Application
;
public
public class
class
ShoppingApplication
ShoppingApplication
extends
extends
Application
{
public
public
ShoppingApplication
() {}
public
public
Set
<
Class
<?>>
getClasses
() {
HashSet
<
Class
<?>>
set
=
new
new
HashSet
<
Class
<?>>();
set
.
add
(
CustomerResource
.
class
);
set
.
add
(
OrderResource
.
class
);
set
.
add
(
ProduceResource
.
class
);
return
return
set
;
}
public
public
Set
<
Object
>
getSingletons
() {
JsonWriter json
=
new
new
JsonWriter
();
CreditCardResource service
=
new
new
CreditCardResource
();
HashSet
<
Object
>
set
=
new
new
HashSet
();
set
.
add
(
json
);
set
.
add
(
service
);
return
return
set
;
}
}
Here, we have a
ShoppingApplication
class that extends the
Application
class. The
getClasses()
method allocates a
HashSet
, populates it with
@Path
annotated classes, and
returns the set. The
getSingletons()
method allocates a
MessageBodyWriter
class named
JsonWriter
and an
@Path
annotated class
CreditCardResource
. It then creates a
HashSet
and adds these instances to it. This set is returned by the method.
Deployment Within a JAX-RS-Aware Container
Java EE stands for Java Enterprise Edition. It is the umbrella specification of JAX-RS and
defines a complete enterprise platform that includes services like a servlet container, EJB,
transaction manager (JTA), messaging (JMS), connection pooling (JCA), database persisten-
ce (JPA), web framework (JSF), and a multitude of other services. Application servers that
are certified under Java EE 6 are required to have built-in support for JAX-RS 1.1. Java EE 7
containers are required to have built-in support for JAX-RS 2.0.