我有一组实体类。 例如Book
和Student
。现在,我想编写提供此类 CRUD 操作的 JAX-RS 类。
@Path("book")
@Stateless
public class BookREST{
@PersistenceContext
private EntityManager em;
@Path("save")
@POST
public void saveBook(Book b){
em.persist(b);
}
@Path("delete")
@DELETE
public void deleteBook(Book b){
em.remove(b);
}
//and so on
}
和
@Path("student")
@Stateless
public class StudentREST{
@PersistenceContext
private EntityManager em;
@Path("save")
@POST
public void saveStudent(Student s){
em.persist(s);
}
@Path("delete")
@DELETE
public void deleteStudent(Student s){
em.remove(s);
}
//and so on
}
所以最后我有几个相同的 JAX-RS 类,除了@Path
注释,如本例中的 @Path("book")
和 @Path("student")
.
有没有更好的方法来编写这样的类?或者甚至有一个工具/maven 插件,我可以传递我的实体类并为我生成 JAX-RS 类?
提前谢谢。
如果您要求webservices from entities
,则可以使用 Netbeans 自动创建它们
New File > Webservice > Restful Webservices from entity classes
如果您正在使用 Eclipse,则可以将 EMF Framework 与 Texo 和 Texo/JSON REST Web Services 一起使用。这样,您可以创建实体模型并从中生成实体。Texo Web 服务可用于对实体执行 CRUD 操作。