Packages, under
resources: ToDoResource, ToDosResource
dao: TodoDao: uses HashMap<String, Todo> to store Todos
model: Todo, with minimal JAXB
markup, just @XmlRootElement on the class.
His Todo
service looks like this:
GET
/todos ßGET to collection URI
GET
/todos/count
POST
/todos
GET
/todos/{id]
PUT
/todos/{id}
DELETE
/todos/{id}
Vogel’s GET /todos returns List<Todo> to
JAX-RS. Here’s the XML:
This is printed in the
tutorial, from a browser:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<todoes>
<todo><id>3</id><summary>Blabla</summary>
</todo>
<todo><description>Read
complete http://www.vogella.de</description>
<id>2</id><summary>Do
something</summary>
</todo>
<todo><description>Read
http://www.vogella.de/articles/REST/article.html</description>
<id>1</id><summary>Learn
REST</summary>
</todo>
</todoes>
Server-side code:
// Return the list of todos to the user in the browser
@GET
@Produces(MediaType.TEXT_XML)
public
List<Todo> getTodosBrowser()
{
List<Todo>
todos = new ArrayList<Todo>();
todos.addAll( TodoDao.instance.getModel().values() );
return
todos;
}
So List<POJO> gets
serialized, using funny plural of todo to contain the
list.
On the client side: set up a service object of type WebResource,
as before, and, for above display
// Get the Todos System.out.println(service.path("rest").path("todos").accept(
MediaType.TEXT_XML).get(String.class));
orderService Business Logic Layer API, i.e. public methods
from package activities
CreateOrderActivity.java: public OrderRepresentation
create(OrderRepresentation orderRep) {
PokeActivity.java: public OrderRepresentation
makeOldestReady() {
ReadOrderActivity.java: public OrderRepresentation
retrieveById(String id) {
RemoveAllOrdersActivity.java:public String delete() {
RemoveOrderActivity.java: public OrderRepresentation
delete(String id) {
UpdateOrderActivity.java: public OrderRepresentation
update(OrderRepresentation
order,
String id)
Exception
classes in package activities:
InvalidOrderException.java
NoSuchOrderException.java
UpdateException.java
NoOrderMatchesException.java
OrderDeletionException.java
throws in package activities:
application-oriented conditions (no HTTP here!)
The
corresponding catches in resources assign the HTTP error code for these
CreateOrderActivity.java: throw new InvalidOrderException();
PokeActivity.java: throw new NoOrderMatchesException();
ReadOrderActivity.java: throw new NoSuchOrderException();
RemoveOrderActivity.java: throw new NoSuchOrderException();
UpdateOrderActivity.java: throw new InvalidOrderException();
UpdateOrderActivity.java: throw new NoSuchOrderException();
UpdateOrderActivity.java: throw new UpdateException();
orderService Data Access Layer API, i.e., public methods
in package repositories
public class OrderRepository {
public static OrderRepository current() {
public Order
get(Identifier identifier) {
public String getFirstNonReadyId() {
public Order
take(Identifier identifier) {
public Identifier
store(Order order) {
public void
store(Identifier orderIdentifier, Order order) {
public boolean has(Identifier identifier) {
public void
remove(Identifier identifier) {
public boolean orderPlaced(Identifier
identifier) {
public boolean orderNotPlaced(Identifier
identifier) {
public String toString() {
public
synchronized void clear() {
public int size() {
Note OrderRepository
objects going through BL API, vs. Order objects going through DA API. The business layer “disguises” its
proprietary Orders before letting them out of the BL layer.
Note that the same BL API can
be used for many different top layers, anything that works with Pull
technology.
GET for order status.
1.
It first shows up
as a call into resources at the JAX-RS @GET method in OrderResource.
2.
This resources
method calls into activities to get status by id: retrieveById(String id)
3.
This activities
method calls into repositories to get the data from long-term storage get(Identifier identifier)
4.
The data is
returned from repositories to activities in an Order object.
5.
Then it is
converted into an OrderRepresentation object and
returned from activities to resources
6.
Then in
resources, the GET method of OrderResource sends it
off as XML.