在上一個(gè)練習(xí)學(xué)習(xí)了如何使用Jersey,以及JAXB來(lái)創(chuàng)建RESTful的web service。 現(xiàn)在我來(lái)結(jié)合后臺(tái)數(shù)據(jù)庫(kù)對(duì)其做升級(jí),也就是通過(guò)Jersey創(chuàng)建用來(lái)修改后臺(tái)數(shù)據(jù)庫(kù)的RESTful web service。 開(kāi)發(fā)環(huán)境: Eclipse Juno, 數(shù)據(jù)庫(kù)MySQL 5.5, Jersey 1.18,EclipseLink 2.4
在上一個(gè)練習(xí)學(xué)習(xí)了如何使用Jersey,以及JAXB來(lái)創(chuàng)建RESTful的web service。
現(xiàn)在我來(lái)結(jié)合后臺(tái)數(shù)據(jù)庫(kù)對(duì)其做升級(jí),也就是通過(guò)Jersey創(chuàng)建用來(lái)修改后臺(tái)數(shù)據(jù)庫(kù)的RESTful web service。
開(kāi)發(fā)環(huán)境:
Eclipse Juno, 數(shù)據(jù)庫(kù)MySQL 5.5, Jersey 1.18,EclipseLink 2.4, JAVA 1.6, 應(yīng)用服務(wù)器Tomcat 7。
1.創(chuàng)建一個(gè)叫做jersey3的Dynamic Web Project。添加JPA的facet。
2.導(dǎo)入Jersey包,導(dǎo)入EclipseLink包,導(dǎo)入MySQL的connect包。
3.開(kāi)發(fā)數(shù)據(jù)庫(kù)對(duì)象,配置JPA。
數(shù)據(jù)對(duì)象Employee類(lèi):
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,編寫(xiě)代碼來(lái)實(shí)現(xiàn)RESTful web service。
創(chuàng)建web.xml:
注意添加了一個(gè)監(jiān)聽(tīng)器LocalEntityManagerFactory,實(shí)現(xiàn)了ServletContextListener類(lèi),它可以在應(yīng)用啟動(dòng)和關(guān)閉的時(shí)候后自動(dòng)調(diào)用。 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
可以來(lái)管理我們的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(); } }
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.準(zhǔn)備幾條測(cè)試數(shù)據(jù),插入到數(shù)據(jù)庫(kù)表中。
6.測(cè)試,這里我用的Chrome瀏覽器插件的POSTMAN來(lái)測(cè)試。
6.1測(cè)試1,單個(gè)查詢
輸入url: http://localhost:8080/jersey3/employee/{id}
6.2測(cè)試2,查詢?nèi)繑?shù)據(jù)
輸入U(xiǎn)RL: http://localhost:8080/jersey3/employee/
小結(jié):
這個(gè)例子很簡(jiǎn)單,配置好JPA和Jersey后,主要就是在Jersey的實(shí)現(xiàn)方法中把數(shù)據(jù)的CRUD需求交給JPA的Entity Manager來(lái)完成就可以了。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com