Java Reference
In-Depth Information
public
public
void
void
writeTo
(
Object o
,
Class type
,
Type genericType
,
Annotation
[]
annotations
,
MediaType mediaType
,
MultivaluedMap httpHeaders
,
OutputStream os
)
throws
throws
IOException
,
WebApplicationException
{
ObjectOutputStream oos
=
new
new
ObjectOutputStream
(
os
);
oos
.
writeObject
(
o
);
}
Like the
readFrom()
method, basic Java serialization is used to marshal our Java object into
the HTTP response body.
The Resource Class
The
CustomerResource
class doesn't change much from
ex06_2
:
src/main/java/com/restfully/shop/services/CustomerResource.java
@Path
(
"/customers"
)
public
public class
class
CustomerResource
CustomerResource
{
...
@POST
@Consumes
(
"
application
/
example
-
java
")
public Response createCustomer(Customer customer)
{
customer.setId(idCounter.incrementAndGet());
customerDB.put(customer.getId(), customer);
System.out.println("
Created customer
" + customer.getId());
return Response.created(URI.create("
/
customers
/
"
+
customer
.
getId
())).
build
();
}
...
}
The code is actually exactly the same as that used in
ex06_1
, except that the
@Produces
and
@Consumes
annotations use the
application/example-java
media type.
The Application Class
The
ShoppingApplication
class needs to change a tiny bit from the previous examples:
src/main/java/com/restfully/shop/services/ShoppingApplication.java