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

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

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

action 类样例 action 类定义一个资源可以实现的所有 HTTP CRUD 操作的实现。Apache Wink 收到 HTTP 请求,然后将一个包装的 HTTP 请求分配到适当的方法(写入 action 类中的)。每个 HTTP 请求应该映射到 action 类的一个方法中,具体根据 HTTP 请求参数、资源方法定义以及 MIME 类型而定。
清单 4 显示如何为 Employee 资源创建一个 action 类。在本示例中,可以创建一个 EmployeeAction 类来实现 Apache Wink RESTful 服务。它由 4 个方法组成,每个代表一个 CRUD 操作。
清单 4. Employee action 类实现
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package rest.action;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import openJpa.mediator.EmployeeOpenJpaMediator;
import openJpa.model._Employee;
import rest.resource.Employee;

@Path(value = "/employee")
public class EmployeeAction {
     
    /**
     * @param employee
     * CREATE: @return This method is used to create new Employee
     */
    @POST
    @Consumes(value = { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    @Produces(value = { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public Response persistEmployee(Employee employee) {
        EmployeeOpenJpaMediator dao = new EmployeeOpenJpaMediator();
        _Employee _employee = employee.getEmployee();
        try {
            dao.beginTransaction();
            dao.persist(_employee);
            dao.commitTransaction();

        }  catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            dao.rollbackTransaction();
            return Response.status(400).entity(
                    "Employee create failed!").build();
        } finally {
            dao.close();
        }
        Employee employeeCreated = new Employee(_employee);
        return Response.ok(employeeCreated).build();
    }
     
    /**
     * @param id
     * RETRIEVE: @return This method is used to retrieve a particular Employee
       through employeeId
     */
    @GET
    @Path("/{id}")
    @Produces(value = { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    public Response findEmployee(@PathParam(value = "id") int id) {

        EmployeeOpenJpaMediator employeeDao = new EmployeeOpenJpaMediator();
        _Employee u = new _Employee();
        try {
        if ((u = (_Employee) employeeDao.getById(_Employee.class, id)) != null) {
            return Response.ok(new Employee(u)).build();
        } else {
            return Response.status(404).entity(
            "Employee id does not exist").build();
        }
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return Response.status(404).entity(
                    "Employee id does not exist").build();
        }
    }
     
    /**
     * @param employee
     * UPDATE: @return this method is used to update a particular Employee
       already existing
     */
    @PUT
    @Produces(value = { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    @Consumes(value = { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public Response updateEmployee(Employee employee) {
        EmployeeOpenJpaMediator ud = new EmployeeOpenJpaMediator();
        _Employee _employee = employee.getEmployee();
        try {
            ud.beginTransaction();
            ud.update(_employee);
            ud.commitTransaction();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            ud.rollbackTransaction();
            return Response.status(409).entity(
                    "Employee update failed!").build();

        } finally {
            ud.close();
        }

        Employee employeeUpdated = new Employee(_employee);
        return Response.ok(employeeUpdated).build();
    }
     
    /**
     * @param id
     * DELETE: This method is used to delete a particular Employee through employeeId
     */
    @DELETE
    @Path("/{id}")
    public Response deleteEmployee(@PathParam(value = "id") int id) {
        EmployeeOpenJpaMediator pd = new EmployeeOpenJpaMediator();
        _Employee _employee = (_Employee) pd.getById(_Employee.class, id);
        if(_employee!=null){
        try {
            pd.beginTransaction();
            pd.delete(_employee);
            pd.commitTransaction();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            pd.rollbackTransaction();
            return Response.status(400).entity(
                    "Employee delete had an error!").build();
        } finally {
            pd.close();
        }
    return Response.ok().build();
        }
        else{
            return Response.status(400).entity(
            "Employee ID does not exist!").build();
        }
    }
}

返回列表