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!