JAX-RS 中@PersistentContext 和@Stateless 的用途是什么? [英] what is the use of @PersistentContext and @Stateless in JAX-RS?

查看:35
本文介绍了JAX-RS 中@PersistentContext 和@Stateless 的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用 JAX-RS 还很陌生.我所经历的教程通过展示如何发出 GET/POST/DELETE 请求,使其变得非常简单.但是没有经过高级注释.现在我正在阅读 Java EE 7 Essentials 书籍.我对这里看到的许多新注释感到困惑.我试图找到这些注释的效用.但我不明白.我一直发现 SO 的答案对于初学者来说很容易理解.

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.

这是来自 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 {
}

  1. EmployeeResource 类有什么用?那是设计模式吗?我可以在 get() 方法中使用 DAO 访问来完成此操作吗?
  2. @PersistentContext@Stateless 是什么意思?我确实在谷歌中搜索了这些注释.但我不相信我理解它
  3. Application 类有什么用?总是需要吗?在我经历的教程中,没有提到 Application 类.同理,@ApplicationPath 注释是什么意思?
  1. What is the use of EmployeeResource class? Is that a design pattern? I could have done this using DAO access in get() method?
  2. what does @PersistentContext and @Stateless mean? I did search for these the annonations in google. but I am not convinced that I understood it
  3. 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?

感谢您的宝贵时间!

推荐答案

  1. EmployeeResource 类有什么用?那是设计模式吗?我可以在 get() 方法中使用 DAO 访问来完成此操作吗?

EmployeeResource 类代表您的 RESTful 服务.从它你将使用 @GET@PUT@POST@DELETE 等公开不同的方法.

The EmployeeResource class represents your RESTful service. From it you are going to expose different methods using @GET, @PUT, @POST, @DELETE etc.

@PersistentContext 和 @Stateless 是什么意思?我确实在谷歌中搜索了这些注释.但我不相信我理解它

@PersistenceContext 是一个 JPA 注释.JPA 涵盖数据库数据与 Java 域模型之间的转换.由于 RESTful 服务通常表示以 Java 域对象为参数的 CRUD(创建、读取、更新、删除)操作,因此使用 JPA 来进行实际的持久化操作非常方便.

@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 是一个 EJB(会话 Bean)注解.这意味着 bean 不能在调用之间保持任何类型的状态.

@Stateless is a EJB (Session Bean) annotation. It means that the bean cannot maintain any sort of state between calls.

Application Class有什么用?总是需要吗?在我经历的教程中,没有提到 Application 类.同理,@ApplicationPath 注解是什么意思?

这是一种拉入未使用 @Provider 注释的片段的有用方法.一种有用的注释是 @ApplicationPath 注释(请参阅:http://docs.oracle.com/javaee/6/api/javax/ws/rs/ApplicationPath.html).这将设置路径的第一部分,然后是在资源类上设置的 @Path,最后是在 REST 操作上设置的 @Path自己.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆