Java Reference
In-Depth Information
public
public class
class
ShoppingApplication
ShoppingApplication
extends
extends
Application
{
private
private
Set
<
Object
>
singletons
=
new
new
HashSet
<
Object
>();
private
private
Set
<
Class
<?>>
classes
=
new
new
HashSet
<
Class
<?>>();
public
public
ShoppingApplication
() {
singletons
.
add
(
new
new
CustomerResource
());
classes
.
add
(
JavaMarshaller
.
class
);
}
@Override
public
public
Set
<
Class
<?>>
getClasses
() {
return
return
classes
;
}
@Override
public
public
Set
<
Object
>
getSingletons
() {
return
return
singletons
;
}
}
For our
Application
class, we need to register the
JavaMarshaller
class. If we don't, the
JAX-RS runtime won't know how to handle the
application/example-java
media type.
The Client Code
The client code isn't much different from
ex06_1
. We just need to modify the
javax.ws.rs.client.Entity
construction to use the
application/example-java
media
type. For example, here's what customer creation looks like:
src/test/java/com/restfully/shop/test/CustomerResourceTest.java
public
public class
class
CustomerResourceTest
CustomerResourceTest
{
@Test
public
public
void
void
testCustomerResource
()
throws
throws
Exception
{
...
Response response
=
client
.
target
(
"http://localhost:8080/services/customers"
)
.
request
().
post
(
Entity
.
entity
(
newCustomer
,
"application/example-java"
));
The static
Entity.entity()
method is called, passing in the plain
Customer
Java object
along with our custom media type.