2012-02-21 75 views
0

我想了解REST和JAX-RS/JAXB,我有許多對多的關係,包括圖書館和書籍。我想對圖書館擁有的圖書進行GET-POST-PUT-DELETE操作,所以GET的url是「libraries/{library_id}/books/{book_id}」 我已經用這種方式實現了:JAX-RS中擁有的資源

@Stateless 
@Path("libraries") 
public class LibraryFacadeREST extends AbstractFacade<Library> { 

    ...  

    @Path("{library_id}/booksFacade/") 
    public BookFacadeREST getBookFacade() { 
    return bookFacadeREST; 
    } 
    ... 
} 

@Stateless 
@Path("books") 
public class BookFacadeREST extends AbstractFacade<Book> { 
    ... 
    @GET 
    @Path("{id}") 
    @Produces({"application/xml", "application/json"}) 
    public Book find(@PathParam("id") Long id, @PathParam("library_id") long libraryId) { 

     Book book = super.find(id); 

     if (libraryId != -1) { 
      Library library = em.find(Library.class, libraryId); 

      if (book.getLibraries().contains(library)) { 
      return book; 
      } else { 
      return null; 
      } 
     } 

     return null; 
    } 
    ... 
} 

但我不喜歡它,它很糟糕,是否有更好的方法來管理jaxrs中的資源?

回答

0

爲什麼其餘的資源需要關係,因爲相關的實體有關係? 我覺得這本書的資源應該是這樣的:

@Path("libraries/{library_id}/books") 
public class BookFacadeREST extends AbstractFacade<Book> { 
    @GET 
    @Path("{id}") 
    @Produces({"application/xml", "application/json"}) 
    public Book find(@PathParam("id") Long id, @PathParam("library_id") long libraryId) { 
     ... 
    } 
} 
+0

是的,我在想同樣的事情。通過這種方式,類更具可讀性。 – Antonio 2012-02-22 13:04:04

相關問題