Hi all, i am learning restful webservices with apache cxf. I came to point, where i am not sure what should be the correct (or good) implementation. I have following pojos:

public class Author {
 
    private Integer id;
    private String firstName;
    private String lastName;
    private String description;
    // getters/setters omitted for simlipicity
}
 
public class Book {
    private Integer id;
    private String title;
    private Author author;
    private String publisher;
    // getters/setters omitted for simlipicity
}

As you can see, author is coupled with book. That is causing a pain in my ... design of services. I followed some books, tutorials and examples of restful services, but i wasnt lucky to find implementation that could answer my questions. for example:

If client inserts new book through webservice (for example as json mapped to this pojo), should I care about the author object inside the book?
The same applies if the client wants to PUT a book - does it mean that the included author should be PUT-ed too?

My attempt was:

// the @Path annotations for the resource are defined on implementing classes ('books' and 'authors')
 
@Produces(MediaType.APPLICATION_JSON)
public interface AuthorService {
 
    @GET
    @Path("{id}")
    public Response findAuthor(@PathParam("id") Integer id);
 
    @GET
    public Response findAllAuthors();
 
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response addAuthor(Author author);
 
    @PUT
    @Path("{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response updateAuthor(@PathParam("id") Integer id, Author author);
 
    @DELETE
    @Path("{id}")
    public Response removeAuthor(@PathParam("id") Integer id);
 
    @GET
    @Path("{id}/books")
    public Response findAllBooksOfAuthor(@PathParam("id") Integer id);
}
 
@Produces(MediaType.APPLICATION_JSON)
public interface BookService {
 
    @GET
    @Path("{id}")
    public Response findBook(@PathParam("id") Integer id);
 
    @GET
    public Response findAllBooks();
 
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response createBook(Book book);
 
    @PUT
    @Path("{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response updateBook(@PathParam("id") Integer id, Book book);
 
    @DELETE
    @Path("{id}")
    public Response removeBook(@PathParam("id") Integer id);
}
 
// example implementation of interface method
 
@Path("books")
public class BookServiceImpl implements BookService {
 
    private BookManagerDao bookDao;
    private AuthorManagerDao authorDao;
 
...
 
    @Override
    public Response createBook(Book book) {
        getAuthorDao().insertAuthor(book.getAuthor());
        getBookDao().insertBook(book);
        URI newBookUri = uriInfo.getAbsolutePathBuilder().path(BookService.class, "findBook").build(book.getId());
        return Response.seeOther(newBookUri).build();
    }
 
    @Override
    public Response updateBook(Integer id, Book book) {
        Book foundBook = bookDao.fetchBookById(id);
 
        if (foundBook == null) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
 
        if (book.getAuthor() != null) {
            try {
                authorDao.updateAuthor(book.getAuthor());
            } catch (NotFoundException nfe) {
                authorDao.insertAuthor(book.getAuthor());
            }
        }
 
        book.setId(foundBook.getId());
        bookDao.updateBook(book);
 
        return Response.noContent().build();
    }
...
 
}

as you see I tried to solve it somehow in webservice, but i dont have the feeling if its enough (what if the author does not have an id (it will be inserted) but it already exists? if the book.getAuthor() in updateBook method is null does it mean it should be cleared from db too?)

and if the structure goes more complicated (store->customer->order->item->manufacturer->address) I cannot imagine i can hold the unified behaviour over the app. If its ok, it is better to implement these decisions in the services methods, or in daoManager methods?


Thats why I am asking for some guidance or if you could point me to some book/blog/sample app. Maybe I am missing some importatn feature in creting services..

I hope the question is clear

thank you