时间:2021-07-01 10:21:17 帮助过:38人阅读
在上一个练习学习了如何使用Jersey,以及JAXB来创建RESTful的web service。 现在我来结合后台数据库对其做升级,也就是通过Jersey创建用来修改后台数据库的RESTful web service。 开发环境: Eclipse Juno, 数据库MySQL 5.5, Jersey 1.18,EclipseLink 2.4
在上一个练习学习了如何使用Jersey,以及JAXB来创建RESTful的web service。
现在我来结合后台数据库对其做升级,也就是通过Jersey创建用来修改后台数据库的RESTful web service。
开发环境:
Eclipse Juno, 数据库MySQL 5.5, Jersey 1.18,EclipseLink 2.4, JAVA 1.6, 应用服务器Tomcat 7。
1.创建一个叫做jersey3的Dynamic Web Project。添加JPA的facet。
2.导入Jersey包,导入EclipseLink包,导入MySQL的connect包。
3.开发数据库对象,配置JPA。
数据对象Employee类:
package sample; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement @Entity @Table(name = "employee") public class Employee { @Id @Column(name = "userId") private Long id; @Column(name = "firstName") private String firstName; @Column(name = "lastName") private String lastName; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
org.eclipse.persistence.jpa.PersistenceProvider sample.Employee
4.配置Jersey,编写代码来实现RESTful web service。
创建web.xml:
注意添加了一个监听器LocalEntityManagerFactory,实现了ServletContextListener类,它可以在应用启动和关闭的时候后自动调用。 RestProjectTest Jersey REST Service com.sun.jersey.spi.container.servlet.ServletContainer com.sun.jersey.config.property.packages sample com.sun.jersey.api.json.POJOMappingFeature true 1 Jersey REST Service /* sample.LocalEntityManagerFactory
可以来管理我们的EntityManager。
package sample; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @WebListener public class LocalEntityManagerFactory implements ServletContextListener { private static EntityManagerFactory emf; @Override public void contextInitialized(ServletContextEvent event) { emf = Persistence.createEntityManagerFactory("EmployeePU"); System.out.println("ServletContextListener started"); } @Override public void contextDestroyed(ServletContextEvent event) { emf.close(); System.out.println("ServletContextListener destroyed"); } public static EntityManager createEntityManager() { if (emf == null) { throw new IllegalStateException("Context is not initialized yet."); } return emf.createEntityManager(); } }开发好JPA相关的持久化功能后,就可以来实现RESTful service了,代码:
package sample; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.Query; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/employee") public class EmployeeController { @GET @Produces({ MediaType.TEXT_XML }) @Path("{id}") public Employee read(@PathParam("id") long id) { long start = System.currentTimeMillis(); System.out.println("EmployeeController.read() started"); EntityManager em = LocalEntityManagerFactory.createEntityManager(); try { return em.find(Employee.class, id); } finally { em.close(); System.out.println("Getting data took " + (System.currentTimeMillis() - start) + "ms."); } } @GET @Produces({ MediaType.TEXT_XML }) public Listread() { long start = System.currentTimeMillis(); System.out.println("EmployeeController.read() started"); EntityManager em = LocalEntityManagerFactory.createEntityManager(); try { Query q = em.createQuery("SELECT e FROM Employee e"); List result = q.getResultList(); return result; } finally { em.close(); System.out.println("Getting data took " + (System.currentTimeMillis() - start) + "ms."); } } }
5.准备几条测试数据,插入到数据库表中。
6.测试,这里我用的Chrome浏览器插件的POSTMAN来测试。
6.1测试1,单个查询
输入url: http://localhost:8080/jersey3/employee/{id}
6.2测试2,查询全部数据
输入URL: http://localhost:8080/jersey3/employee/
小结:
这个例子很简单,配置好JPA和Jersey后,主要就是在Jersey的实现方法中把数据的CRUD需求交给JPA的Entity Manager来完成就可以了。