在上一个练习学习了如何使用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; } }
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="EmployeePU" transaction-type="RESOURCE_LOCAL"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <class>sample.Employee</class> <properties> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:8889/test" /> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> <property name="javax.persistence.jdbc.user" value="root" /> <property name="javax.persistence.jdbc.password" value="root" /> </properties> </persistence-unit> </persistence>
4.配置Jersey,编写代码来实现RESTful web service。
创建web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>RestProjectTest</display-name> <servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>sample</param-value> </init-param> <init-param> <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <listener> <listener-class>sample.LocalEntityManagerFactory</listener-class> </listener></span> </web-app>
可以来管理我们的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 List<Employee> read() { 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<Employee> 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来完成就可以了。