最近在项目中,因为几个系统自己需要数据交换,所以采用进来都比较流行的RestFul风格WebService,实现框架采用apache的cxf,apache的东西一直以来都是比较的好用,回话少说,进入正题。
首先,提出两个问题,已经解决方案,后面跟上代码。
1、cxf中如何实现java中泛型的数据序列化和反序列化(通常使用json、xml格式,cxf默认不支持泛型)
2、cxf后台异常,在前台如何反映
问题1答案比较简单,cxf中的数据序列化是可以替换掉使用你实现MessageBodyReader
问题2同样的比较简单,因为基于http的restful实现时,服务器返回数据的时候都会告诉客户端一个响应状态吗,就是我们常看到的200、404、500等,cxf框架的rs webservice客户端实现是通过判断状态大于等于300时,抛出异常webapplicationexception,所以如果服务器端有异常时,通过设置状态就可以实现,并返回Response(通过实现ExceptionMapper
代码:
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
@XmlRootElement
(name=
"Customer"
)
public
class
Customer {
private
String id;
private
String name;
private
Date birthday;
public
String getId() {
return
id;
}
public
void
setId(String id) {
this
.id = id;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
Date getBirthday() {
return
birthday;
}
public
void
setBirthday(Date birthday) {
this
.birthday = birthday;
}
}
@XmlRootElement
(name=
"Me"
)
public
class
Me
implements
Serializable {
private
String name;
/**
* @return the name
*/
public
String getName() {
return
name;
}
/**
* @param name the name to set
*/
public
void
setName(String name) {
this
.name = name;
}
}
@XmlRootElement
(name =
"Page"
)
@XmlSeeAlso
({Customer.
class
,Me.
class
})
public
class
Page
implements
Serializable{
/**
*
*/
private
static
final
long
serialVersionUID = 5859907455479273251L;
public
static
int
DEFAULT_PAGE_SIZE =
10
;
private
int
pageSize = DEFAULT_PAGE_SIZE;
// 每页的记录数
private
long
start;
// 当前页第一条数据在List中的位置,从0开始
private
List
// 当前页中存放的记录,类型一般为List
private
long
totalCount;
// 总记录数
/**
* 构造方法,只构造空页.
*/
public
Page() {
this
(
0
,
0
, DEFAULT_PAGE_SIZE,
new
ArrayList
}
public
Page(
int
pageSize) {
this
(
0
,
0
, pageSize,
new
ArrayList
}
/**
* 默认构造方法.
*
* @param start
* 本页数据在数据库中的起始位置
* @param totalSize
* 数据库中总记录条数
* @param pageSize
* 本页容量
* @param data
* 本页包含的数据
*/
public
Page(
long
start,
long
totalSize,
int
pageSize, List
this
.pageSize = pageSize;
this
.start = start;
this
.totalCount = totalSize;
this
.data = data;
}
/**
* 取总记录数.
*/
public
long
getTotalCount() {
return
this
.totalCount;
}
/**
* 取总页数.
*/
public
long
getTotalPageCount() {
if
(totalCount % pageSize ==
0
)
return
totalCount / pageSize;
else
return
totalCount / pageSize +
1
;
}
/**
* 取每页数据容量.
*/
public
int
getPageSize() {
return
pageSize;
}
/**
* 取当前页中的记录.
*/
public
List
return
data;
}
/**
* 取该页当前页码,页码从1开始.
*/
public
long
getCurrentPageNo() {
return
start / pageSize +
1
;
}
/**
* 该页是否有下一页.
*/
public
boolean
hasNextPage() {
return
this
.getCurrentPageNo() <
this
.getTotalPageCount();
}
/**
* 该页是否有上一页.
*/
public
boolean
hasPreviousPage() {
return
this
.getCurrentPageNo() >
1
;
}
/**
* 获取任一页第一条数据在数据集的位置,每页条数使用默认值.
*
* @see #getStartOfPage(int,int)
*/
protected
static
int
getStartOfPage(
int
pageNo) {
return
getStartOfPage(pageNo, DEFAULT_PAGE_SIZE);
}
/**
* 获取任一页第一条数据在数据集的位置.
*
* @param pageNo
* 从1开始的页号
* @param pageSize
* 每页记录条数
* @return 该页第一条数据
*/
public
static
int
getStartOfPage(
int
pageNo,
int
pageSize) {
return
(pageNo -
1
) * pageSize;
}
@Override
public
String toString() {
return
ToStringBuilder.reflectionToString(
this
,
ToStringStyle.SHORT_PREFIX_STYLE);
}
/**
* @return the start
*/
public
long
getStart() {
return
start;
}
/**
* @param start the start to set
*/
public
void
setStart(
long
start) {
this
.start = start;
}
/**
* @return the data
*/
public
List
return
data;
}
/**
* @param data the data to set
*/
public
void
setData(List
this
.data = data;
}
/**
* @param pageSize the pageSize to set
*/
public
void
setPageSize(
int
pageSize) {
this
.pageSize = pageSize;
}
/**
* @param totalCount the totalCount to set
*/
public
void
setTotalCount(
long
totalCount) {
this
.totalCount = totalCount;
}
}
|
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
|
public
class
ServiceException
extends
RuntimeException {
private
static
final
long
serialVersionUID = 7607640803750403555L;
public
ServiceException() {
super
();
}
public
ServiceException(String message) {
super
(message);
}
public
ServiceException(String message, Throwable cause) {
super
(message, cause);
}
public
ServiceException(Throwable cause) {
super
(cause);
}
}
|
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
|
@Path
(value =
"/customer"
)
@Produces
({
"application/xml"
,
"application/json"
})
public
interface
CustomerService {
@GET
@Path
(value =
"/{id}/info"
)
Customer findCustomerById(
@PathParam
(
"id"
)String id);
@GET
@Path
(value =
"/search"
)
Customer findCustomerByName(
@QueryParam
(
"name"
)String name);
@POST
@Path
(value =
"/all"
)
List
@POST
@Path
(value =
"/page"
)
Page
throws
ServiceException;
@POST
@Path
(value =
"/pageMe"
)
Page
}
public
class
CustomerServiceImpl
implements
CustomerService {
public
Customer findCustomerById(String id) {
Customer customer =
new
Customer();
customer.setId(id);
customer.setName(id);
customer.setBirthday(Calendar.getInstance().getTime());
return
customer;
}
public
Customer findCustomerByName(String name) {
Customer customer =
new
Customer();
customer.setId(name);
customer.setName(name);
customer.setBirthday(Calendar.getInstance().getTime());
return
customer;
}
/** (non-Javadoc)
* @see edu.xdev.restful.CustomerService#findAllCustomer()
*/
@Override
public
List
List
new
LinkedList
Customer customer =
new
Customer();
customer.setId(
"e24234"
);
customer.setName(
"張三"
);
customer.setBirthday(Calendar.getInstance().getTime());
tar.add(customer);
customer =
new
Customer();
customer.setId(
"324324"
);
customer.setName(
"李四"
);
customer.setBirthday(Calendar.getInstance().getTime());
tar.add(customer);
return
tar;
}
/** (non-Javadoc)
* @see edu.xdev.restful.CustomerService#findPageCustomer()
*/
public
Page
throws
ServiceException {
List
new
LinkedList
Customer customer =
new
Customer();
customer.setId(
"e24234"
);
customer.setName(
"張三"
);
customer.setBirthday(Calendar.getInstance().getTime());
tar.add(customer);
customer =
new
Customer();
customer.setId(
"324324"
);
customer.setName(
"李四"
);
customer.setBirthday(Calendar.getInstance().getTime());
tar.add(customer);
Page
new
Page
1
,
2
,
1
, tar);
if
(
1
==
1
){
throw
new
ServiceException(
"abcd"
);
}
return
page;
}
/** (non-Javadoc)
* @see edu.xdev.restful.CustomerService#findPage()
*/
public
Page
List
new
LinkedList
Me m =
new
Me();
m.setName(
"中文"
);
|