我正在尝试理解REST和JAX-RS / JAXB,我与图书馆和书籍之间存在多对多的关系。我想在一个由图书馆拥有的图书上进行GET-POST-PUT-DELETE操作,因此GET的网址是“libraries / {library_id} / books / {book_id}” 我已经用这种方式实现了它:
@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 :(得分: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) {
...
}
}