首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

使用 Apache Wink 和 OpenJPA 构建 REST 资源(1)

使用 Apache Wink 和 OpenJPA 构建 REST 资源(1)

简介具象状态传输 (Representational State Transfer,REST) 是分布式超媒体系统(比如 World Wide Web)的一种软件构架风格。资源或者特定信息来源是 REST 的一个重要组成部分。每一资源通过全局标识符(例如,HTPP 中的 URI)引用。想要对资源进行操作,网路组件(称为用户代理和伺服器)使用标准化接口 (HTTP) 通信以及交换资源表现方式。
常见缩略词
  • CRUD:创建、检索、更新、删除
  • HTTP:超文本传输协议
  • REST:具象状态传输
  • URI:统一资源标志符
  • XML:可扩展标记语言

Apache Wink 是一个框架,您可用来构建 RESTful Web 服务。根据 REST 体系结构风格,Wink 通过提供构建服务的方式促进 REST Web 服务的开发和消耗。Wink 提供必要的架构来定义和实现资源、表现形式和构成服务的统一方法。Apache OpenJPA 框架是一个开源软件,定义了一个资源如何实现持久化。
在本文中,使用 Wink 和 OpenJPA 通过一个 REST 服务在资源上实现 HTTP 操作,在一个样例资源中学习建模、URI 设计以及使用 Wink 注释实现创建、检索、更新和删除 (CRUD) 操作。
您可以在此  本文所用源代码。
资源建模每个资源必须被建模成一个具体结构,比如 XML 和 JSON。该资源的所有属性和值将使用相应 XML 属性名和值映射。以本文为例,让我们考虑将 “Employee” 作为一个资源,每个 Employee 拥有惟一的属性,比如员工 ID,员工名字、员工地址、员工邮箱、以及员工电话号码。清单 1 显示了 Employee 资源的 XML 结构。               
清单 1. Employee 资源的 XML 结构
1
2
3
4
5
6
7
<employee>        
     <employeeId>101</employeeId>
     <employeeName>employee Name</employeeName>
     <address>XYZ C Block, Bangalore-560025</address>
     <email>employee email</email>
     <telephone>5551234567</telephone>
</employee>




Apache Wink 框架支持 JAXB 注释来将 XML 或 JSON 编组和解组到一个 JAVA 对象中,反之亦然。OpenJPA 为持久化 JAVA 对象进入关系型数据存储提供一个框架。
实体建模 OpenJPA 中的实体是持久化对象,表示数据存储记录。如本文示例所示,_Employee 是实体模型,表示 Employee 对象。实体类 _Employee 的每个持久化实例表示一个惟一的数据存储记录(惟一员工)。您可以使用注释定义实体模型。在 Employee 案例中,@Table (name="CF_EMPLOYEE") 用于定义 Employee 实体。
所有实体类必须声明一个或多个字段,共同构成一个实例的持久化标识。在我们的示例 employee 中,employeeId 被用作惟一标识符。OpenJPA 使用实体中的 version 字段检测同一数据存储记录的并行修改。使用 @Version 注释。清单 2 中的代码摘要显示了如何为 Employee 资源创建一个实体模型。
清单 2. Employee 资源的 OpenJPA 模型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package openJpa.model;

/**
* Persistent Class for Employee
* OpenJPA enables writing persistent classes in a very transparent way
*/

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Version;

@Entity
/*Each entity represents a relational datastore record.
In this code all employee records will be stored in a table called CF_EMPLOYEE */
@Table(name="CF_EMPLOYEE")
/*
Databases allow Native sequences to be created , which are database
structures that generates increasing numeric values.
SequenceGenerator annotation represents a named database Sequence
*/
@SequenceGenerator(name="employeeSeqGen", sequenceName="native(Sequence=CF_EMPLOYEE_SEQ)")
public class _Employee implements Serializable {
     
    private static final long serialVersionUID = 1L;
    public _Employee() {
        super();
    }
     
/*All entity classes must declare one or more fields which together
forms the persistent identity of an instance.Every employee will have an
unique employeeID which is the persistent identity */
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="employeeSeqGen")
    private int employeeId;
/* OPEN JPA uses version field in entities to detect concurrent
modifications to the same datastore record.Version field is optional
for any entity, but without one concurrent threads or processes might succeed
in making conflicting changes to the same record at the same time. */

    @Version
    private int version;
     
    private String employeeName;
    private String address;
    private String email;
    private int telephone;
     
    public void setEmployeeId(int employeeId) {
        this.employeeId = employeeId;
    }
    public int getEmployeeId() {
        return employeeId;
    }
    public void setVersion(int version) {
        this.version = version;
    }
    public int getVersion() {
        return version;
    }
    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }
    public String getEmployeeName() {
        return employeeName;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getAddress() {
        return address;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getEmail() {
        return email;
    }
    public void setTelephone(int telephone) {
        this.telephone = telephone;
    }
    public int getTelephone() {
        return telephone;
    }
}

返回列表