EclipseLink JPA-RS简介
在之前的系列文章中,我介紹了如何創(chuàng)建一個(gè)將JPA用于持久層的JAX-RS服務(wù)。 EclipseLink包含一個(gè)名為JPA-RS的組件,該組件可用于輕松自動(dòng)地將持久性單元公開為RESTful服務(wù)(支持XML和JSON消息)。 MOXy為JPA-RS提供XML和JSON綁定,并且雙向映射之類的東西會(huì)自動(dòng)為您映射。 在另一篇文章中,我將介紹如何使用MOXy定制本示例中顯示的消息。
我將使用在以下帖子中創(chuàng)建的JPA模型:
- 第1部分–數(shù)據(jù)庫(kù)
- 第2部分–將數(shù)據(jù)庫(kù)映射到JPA實(shí)體
- 注解
包裝/部署
使用JPA-RS是一個(gè)簡(jiǎn)單的包裝問(wèn)題。 我們將創(chuàng)建一個(gè)WAR,其中包含JAR中的JPA模型,JPA-RS JAR和用于初始化JPA模型的簡(jiǎn)單會(huì)話bean。 對(duì)于此示例,我使用的是包含EclipseLink 2.5的GlassFish 4.0的升級(jí)版本。
- http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/
客戶JPARS.war
- 網(wǎng)絡(luò)信息
- 類
- META-INF
- 清單文件
持久性WeaverBean
JPA-RS要求已初始化JPA實(shí)體。 我們將創(chuàng)建一個(gè)簡(jiǎn)單的會(huì)話bean來(lái)完成此任務(wù)。
package org.example.ejb;import javax.ejb.*; import javax.persistence.*;@Startup @Singleton @LocalBean public class PersistenceWeaverBean {@SuppressWarnings("unused")@PersistenceUnit(unitName = "CustomerService")private EntityManagerFactory emf;}客戶JPA.jar
該JAR包含我們?cè)谝韵挛恼轮卸x的JPA模型:
- 第2部分–將數(shù)據(jù)庫(kù)映射到JPA實(shí)體
- 注解
org.eclipse.persistence.jpars_2.5.0.qualifier.jar
這是來(lái)自EclipseLink安裝的JPA-RS JAR:
(服務(wù)元數(shù)據(jù))
一旦部署了WAR,我們的服務(wù)就會(huì)啟動(dòng)。 我們可以執(zhí)行GET來(lái)查看我們服務(wù)的元數(shù)據(jù)。
GET(應(yīng)用程序/ xml或應(yīng)用程序/ json)
用于獲取服務(wù)元數(shù)據(jù)的URI具有以下格式:
http://{Server}/{Application}/persistence/v1.0/{PersistenceUnit}/metadata以下是我們示例的URI:
http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/metadata響應(yīng)
以下是我們服務(wù)的元數(shù)據(jù)。 除了持久性單元名稱之外,我們還看到JPA模型中每個(gè)實(shí)體的元數(shù)據(jù)鏈接。 接下來(lái),我們將仔細(xì)研究Customer實(shí)體。
(實(shí)體元數(shù)據(jù))
如果我們點(diǎn)擊其中一個(gè)實(shí)體的鏈接,那么我們將獲得以下信息:
- 實(shí)體屬性。
- 我們可以在實(shí)體上執(zhí)行的CRUD操作。
- 我們可以在實(shí)體上執(zhí)行的命名查詢。
GET(應(yīng)用程序/ xml或應(yīng)用程序/ json)
用于獲取實(shí)體元數(shù)據(jù)的URI具有以下格式:
http://{Server}/{Application}/persistence/v1.0/{PersistenceUnit/metadata/entity/{Entity}以下是獲取Customer實(shí)體的元數(shù)據(jù)的URI:
http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/metadata/entity/Customer響應(yīng)
以下是客戶實(shí)體的元數(shù)據(jù)。 我們將仔細(xì)研究POST操作(第37-39行)和命名查詢(第49-58行)。
{"name": "Customer","attributes": [{"name": "id","type": "long"},{"name": "firstName","type": "String"},{"name": "lastName","type": "String"},{"name": "address","type": "Address"},{"name": "phoneNumbers","type": "Set<PhoneNumber>"}],"linkTemplates": [{"method": "get","href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer/{primaryKey}","rel": "find"},{"method": "put","href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer","rel": "persist"},{"method": "post","href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer","rel": "update"},{"method": "delete","href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer/{primaryKey}","rel": "delete"}],"queries": [{"queryName": "findCustomersByCity","returnTypes": ["Customer"],"linkTemplate": {"method": "get","href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/query/findCustomersByCity;city={city}","rel": "execute"},"jpql": "SELECT c FROM Customer c WHERE c.address.city = :city"}] }實(shí)體存在
我們將使用POST操作創(chuàng)建Customer實(shí)體的新實(shí)例。
POST(應(yīng)用程序/ xml或應(yīng)用程序/ json)
用于創(chuàng)建實(shí)體的URI具有以下格式:
http://{Server}/{Application}/persistence/v1.0/{PersistenceUnit}/entity/{Entity}以下是用于創(chuàng)建Customer實(shí)例的URI:
http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer請(qǐng)求
以下是我們將發(fā)布到上述URI中的客戶數(shù)據(jù)的JSON表示形式:
執(zhí)行查詢
JPA-RS會(huì)為我們?cè)贘PA模型中定義的每個(gè)命名查詢自動(dòng)創(chuàng)建URI:
GET(應(yīng)用程序/ xml或應(yīng)用程序/ json)
執(zhí)行命名查詢的URI具有以下格式:
http://{Server}/{Application}/persistence/v1.0/{PersistenceUnit}/query/{NamedQuery;Parameters}下面,我們將調(diào)用名為findCustomersByCity的查詢,以查找來(lái)自Any Town的所有客戶。
http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/query/findCustomersByCity;city=Any%20Town響應(yīng)
以下是調(diào)用命名查詢的結(jié)果。 我們可以看到實(shí)體之間的關(guān)系表示為鏈接。 您在鏈接上執(zhí)行GET以獲取引用的數(shù)據(jù)。
[{"firstName": "Jane","id": 1,"lastName": "Doe","_relationships": [{"_link": {"href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer/1/address","rel": "address"}},{"_link": {"href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer/1/phoneNumbers","rel": "phoneNumbers"}}],"address": {"_link": {"href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Address/1","method": "GET","rel": "self"}},"phoneNumbers": [{"_link": {"href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/PhoneNumber/3","method": "GET","rel": "self"}},{"_link": {"href": "http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/PhoneNumber/2","method": "GET","rel": "self"}}]} ] 參考:在Java XML和JSON綁定博客上,我們的JCG合作伙伴 Blaise Doughan 介紹了EclipseLink JPA-RS 。翻譯自: https://www.javacodegeeks.com/2013/04/introducing-eclipselink-jpa-rs.html
總結(jié)
以上是生活随笔為你收集整理的EclipseLink JPA-RS简介的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 三星堆在哪(三星堆的遗址位于哪座城市的什
- 下一篇: 使用Maven安装本地jar