I am new to web services. Please give suggestions how to insert and retrieve data from database using jersey JAX - RS in java?
Below is an example of a JAX-RS service implemented as a session bean using JPA for persistence and JAXB for messaging might look like.
CustomerService
package org.example;
import java.util.List;
import javax.ejb.*;
import javax.persistence.*;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Stateless
@LocalBean
@Path("/customers")
public class CustomerService {
@PersistenceContext(unitName="CustomerService",
type=PersistenceContextType.TRANSACTION)
EntityManager entityManager;
@POST
@Consumes(MediaType.APPLICATION_XML)
public void create(Customer customer) {
entityManager.persist(customer);
}
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("{id}")
public Customer read(@PathParam("id") long id) {
return entityManager.find(Customer.class, id);
}
@PUT
@Consumes(MediaType.APPLICATION_XML)
public void update(Customer customer) {
entityManager.merge(customer);
}
@DELETE
@Path("{id}")
public void delete(@PathParam("id") long id) {
Customer customer = read(id);
if(null != customer) {
entityManager.remove(customer);
}
}
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("findCustomersByCity/{city}")
public List findCustomersByCity(@PathParam("city") String city) {
Query query = entityManager.createNamedQuery("findCustomersByCity");
query.setParameter("city", city);
return query.getResultList();
}
}
Customer
Below is an example of one of the entities. It contains both JPA and JAXB annotations.
package org.example;
import java.io.Serializable;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Set;
@Entity
@NamedQuery(name = "findCustomersByCity",
query = "SELECT c " +
"FROM Customer c " +
"WHERE c.address.city = :city")
@XmlRootElement
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private long id;
@Column(name="FIRST_NAME")
private String firstName;
@Column(name="LAST_NAME")
private String lastName;
@OneToOne(mappedBy="customer", cascade={CascadeType.ALL})
private Address address;
@OneToMany(mappedBy="customer", cascade={CascadeType.ALL})
private Set phoneNumbers;
}
For More Information
UPDATE
what are the jars required
You can deploy a JAX-RS/EJB/JPA/JAXB application to any Java EE 6 compliant application server without requiring any additional server set up. Programming the client you can get the JAX-RS APIs from the Jersey (http://jersey.java.net/), and the JPA and JAXB APIs from EclipseLink (http://www.eclipse.org/eclipselink/).
and where the database connections is written
JDBC Resource & Connection Pool
You need to configure a connection pool on your application server. Below are the steps to do this on GlassFish. The steps will vary depending on the application server you are using.
JPA Configuration
Then we reference the database connection we created above in the persistence.xml
file for our JPA entities as follows:
org.eclipse.persistence.jpa.PersistenceProvider
CustomerService
org.example.Customer
org.example.Address
org.example.PhoneNumber