长春网站建设模板样式,网站等保测评,吃什么补肾壮阳,答题卡在线制作网站在之前的系列文章中#xff0c;我介绍了如何创建一个将JPA用于持久层的JAX-RS服务。 EclipseLink包含一个名为JPA-RS的组件#xff0c;该组件可用于轻松自动地将持久性单元公开为RESTful服务#xff08;支持XML和JSON消息#xff09;。 MOXy为JPA-RS提供XML和JSON绑定… 在之前的系列文章中我介绍了如何创建一个将JPA用于持久层的JAX-RS服务。 EclipseLink包含一个名为JPA-RS的组件该组件可用于轻松自动地将持久性单元公开为RESTful服务支持XML和JSON消息。 MOXy为JPA-RS提供XML和JSON绑定并且双向映射之类的东西会自动为您映射。 在另一篇文章中我将介绍如何使用MOXy定制本示例中显示的消息。 我将使用在以下帖子中创建的JPA模型 第1部分–数据库 第2部分–将数据库映射到JPA实体 注解 包装/部署 使用JPA-RS是一个简单的包装问题。 我们将创建一个WAR其中包含JAR中的JPA模型JPA-RS JAR和用于初始化JPA模型的简单会话bean。 对于此示例我使用的是包含EclipseLink 2.5的GlassFish 4.0的升级版本。 http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/ 客户JPARS.war 网络信息 类 META-INF 清单文件 持久性WeaverBean JPA-RS要求已初始化JPA实体。 我们将创建一个简单的会话bean来完成此任务。 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包含我们在以下文章中定义的JPA模型 第2部分–将数据库映射到JPA实体 注解 org.eclipse.persistence.jpars_2.5.0.qualifier.jar 这是来自EclipseLink安装的JPA-RS JAR ECLIPSELINK_HOME/jlib/jpa/org.eclipse.persistence.jpars_2.5.0.qualifier.jar服务元数据 一旦部署了WAR我们的服务就会启动。 我们可以执行GET来查看我们服务的元数据。 GET应用程序/ xml或应用程序/ json 用于获取服务元数据的URI具有以下格式 http://{Server}/{Application}/persistence/v1.0/{PersistenceUnit}/metadata 以下是我们示例的URI http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/metadata 响应 以下是我们服务的元数据。 除了持久性单元名称之外我们还看到JPA模型中每个实体的元数据链接。 接下来我们将仔细研究Customer实体。 {persistenceUnitName: CustomerService,types: [{_link: {href: http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/metadata/entity/Address,method: application/json,rel: Address}},{_link: {href: http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/metadata/entity/PhoneNumber,method: application/json,rel: PhoneNumber}},{_link: {href: http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/metadata/entity/Customer,method: application/json,rel: Customer}}]
}实体元数据 如果我们点击其中一个实体的链接那么我们将获得以下信息 实体属性。 我们可以在实体上执行的CRUD操作。 我们可以在实体上执行的命名查询。 GET应用程序/ xml或应用程序/ json 用于获取实体元数据的URI具有以下格式 http://{Server}/{Application}/persistence/v1.0/{PersistenceUnit/metadata/entity/{Entity} 以下是获取Customer实体的元数据的URI http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/metadata/entity/Customer 响应 以下是客户实体的元数据。 我们将仔细研究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: SetPhoneNumber}],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}]
}实体存在 我们将使用POST操作创建Customer实体的新实例。 POST应用程序/ xml或应用程序/ json 用于创建实体的URI具有以下格式 http://{Server}/{Application}/persistence/v1.0/{PersistenceUnit}/entity/{Entity} 以下是用于创建Customer实例的URI http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/entity/Customer 请求 以下是我们将发布到上述URI中的客户数据的JSON表示形式 {id : 1,firstName : Jane,lastName : Doe,address : {id : 1,street : 1 A Street,city : Any Town},phoneNumbers : [{id : 2,type : work,num : 555-1111}, {id : 3,type : home,num : 555-2222}]
}执行查询 JPA-RS会为我们在JPA模型中定义的每个命名查询自动创建URI GET应用程序/ xml或应用程序/ json 执行命名查询的URI具有以下格式 http://{Server}/{Application}/persistence/v1.0/{PersistenceUnit}/query/{NamedQuery;Parameters} 下面我们将调用名为findCustomersByCity的查询以查找来自Any Town的所有客户。 http://localhost:8080/CustomerJPARS/persistence/v1.0/CustomerService/query/findCustomersByCity;cityAny%20Town 响应 以下是调用命名查询的结果。 我们可以看到实体之间的关系表示为链接。 您在链接上执行GET以获取引用的数据。 [{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