什么是JAX-RS采用@PersistentContext和@Stateless的? [英] what is the use of @PersistentContext and @Stateless in JAX-RS?
问题描述
我是相当新的使用JAX-RS。我所经历的教程使得它非常简单的通过展示如何让 GET / POST / DELETE
请求。但没通过先进的注解去。现在,我读了Java EE 7要领书。我很困惑着许多新的注解,我在这里看到。我试图找到这些注释的效用。但我不明白。我一直觉得SO答案,对初学者很容易理解的。
下面是从<一的code href=\"https://github.com/javaee-samples/javaee7-samples/tree/master/jaxrs/db-access/src/main/java/org/javaee7/jaxrs/dbaccess\"相对=nofollow> github上:
Employee.Java
@Entity
@Table(NAME =REST_DB_ACCESS)
@NamedQueries({
@NamedQuery(NAME =Employee.findAll,查询=选择E从员工E)
})
@XmlRootElement
公共类员工实现Serializable {
私有静态最后的serialVersionUID长1L =;
@ID
@GeneratedValue(策略= GenerationType.AUTO)
私人诠释身份证;
@Column(长度= 40)
私人字符串名称;
公务员(){}
公务员(字符串名称){
this.name =名称;
}
公众诠释的getId(){
返回ID;
}
公共无效SETID(INT ID){
this.id = ID;
}
公共字符串的getName(){
返回名称;
}
公共无效setname可以(字符串名称){
this.name =名称;
}
@覆盖
公共字符串的toString(){
返回名称++身份证;
}
@覆盖
公共布尔等于(obj对象){
如果(空== OBJ)
返回false;
如果(!(OBJ的instanceof员工))
返回false;
员工认为=(雇员)目标文件;
如果(that.name.equals(this.name)及&放大器; that.id == this.id)
返回true;
其他
返回false;
}
@覆盖
公众诠释哈希code(){
返回Objects.hash(this.id,this.name);
}
}
EmployeeResource.Java
@Path(雇员)
@Stateless
公共类EmployeeResource {
@PersistenceContext
EntityManager的EM;
@得到
@Produces(应用程序/ XML)
公务员[]得到(){
返回em.createNamedQuery(Employee.findAll,Employee.class).getResultList()的toArray(新员工[0])。
}
}
MyApplication.java
@ javax.ws.rs.ApplicationPath(webresources)
公共类MyApplication的扩展应用{
}
- 什么是
类使用 EmployeeResource的?那是设计模式呢?我可以做这个使用DAO访问
的get()
方法? - 什么
@PersistentContext
和@Stateless
是什么意思?我做了搜索在谷歌这些的annonations。但我不相信,我的理解是 - 有什么用
应用
类的?是否总是需要的?在我所经历的教程,并没有提到关于应用
类。在同样的道理,什么是@ApplicationPath
注释是什么意思?
感谢您的时间!
-
的有什么用EmployeeResource类的?那是设计模式呢?我可以做在的get()这个使用DAO访问方法?的
的
EmployeeResource
类重presents您的RESTful服务。从中你会使用公开不同的方法@GET
,@PUT
,@POST
,@DELETE
等。 -
的什么@PersistentContext和@Stateless是什么意思?我做了搜索在谷歌这些的annonations。但我不相信,我的理解是的
@PersistenceContext
是一个JPA注释。 JPA覆盖数据库中的数据向/从一个Java域模型的转换。由于RESTful服务通常重新present CRUD(创建,读取,更新,删除)与Java领域对象的操作作为参数是使用JPA做实际的持久化操作非常方便。@Stateless
是一个EJB(会话Bean)注释。这意味着,绿豆不能保持任何形式的调用之间的状态。 -
的什么用应用程序类的?是否总是需要的?在我所经历的教程,并没有提到有关应用程序类。在同样的道理,是什么@ApplicationPath注解是什么意思?的
这是不与
@Provider
注释件拉一个有用的方法。一个有用的注解是@ApplicationPath
标注(见:的 http://docs.oracle.com/javaee/6/api/javax/ws/rs/ApplicationPath.html )。这将设置路径的第一部分的一部分,这是其次是@Path
设置在资源类,最后是在@Path
在REST操作本身设置的。
I am fairly new to using JAX-RS. The tutorials I went through made it really simple by showing how to make GET/POST/DELETE
requests. But did not go through advanced annotations. Now I am reading the Java EE 7 essentials book. I am confused with many new annotations that I see here. I tried to find the utility of these annotations. but I did not understand. I have always found SO answers to be easily understandable for beginners.
Here is the code from github:
Employee.Java
@Entity
@Table(name = "REST_DB_ACCESS")
@NamedQueries({
@NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e")
})
@XmlRootElement
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(length=40)
private String name;
public Employee() { }
public Employee(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name + " " + id;
}
@Override
public boolean equals(Object obj) {
if (null == obj)
return false;
if (!(obj instanceof Employee))
return false;
Employee that = (Employee)obj;
if (that.name.equals(this.name) && that.id == this.id)
return true;
else
return false;
}
@Override
public int hashCode() {
return Objects.hash(this.id, this.name);
}
}
EmployeeResource.Java
@Path("employee")
@Stateless
public class EmployeeResource {
@PersistenceContext
EntityManager em;
@GET
@Produces("application/xml")
public Employee[] get() {
return em.createNamedQuery("Employee.findAll", Employee.class).getResultList().toArray(new Employee[0]);
}
}
MyApplication.java
@javax.ws.rs.ApplicationPath("webresources")
public class MyApplication extends Application {
}
- What is the use of
EmployeeResource
class? Is that a design pattern? I could have done this using DAO access inget()
method? - what does
@PersistentContext
and@Stateless
mean? I did search for these the annonations in google. but I am not convinced that I understood it - what is the use of
Application
Class? Is it always needed? In the tutorial I went through, no mention was made aboutApplication
class. On the same token, what does@ApplicationPath
annotation mean?
Thank you for your time!
What is the use of EmployeeResource class? Is that a design pattern? I could have done this using DAO access in get() method?
The
EmployeeResource
class represents your RESTful service. From it you are going to expose different methods using@GET
,@PUT
,@POST
,@DELETE
etc.what does @PersistentContext and @Stateless mean? I did search for these the annonations in google. but I am not convinced that I understood it
@PersistenceContext
is a JPA annotation. JPA covers the conversion of database data to/from a Java domain model. Since RESTful services generally represent CRUD (create, read, update, delete) operations with Java domain objects as parameters it is very convenient to use JPA to do the actual persistence operations.@Stateless
is a EJB (Session Bean) annotation. It means that the bean cannot maintain any sort of state between calls.what is the use of Application Class? Is it always needed? In the tutorial I went through, no mention was made about Application class. On the same token, what does @ApplicationPath annotation mean?
It is a useful way to pull in pieces that aren't annotated with
@Provider
. One useful annotation is the@ApplicationPath
annotation (see: http://docs.oracle.com/javaee/6/api/javax/ws/rs/ApplicationPath.html). This sets the first part part of the path, this is followed by the@Path
set on the resource class, which is finally followed by the@Path
set on the REST operation itself.
这篇关于什么是JAX-RS采用@PersistentContext和@Stateless的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!