(1)项目结构:
在这里,我没有新建一个项目,而是在上一个项目的基础上实现的,具体来说就是新建了3个文件,如下图所示:
其中,User类是一个普通的实体类,RestService和RestServiceImpl类分别表示服务接口和它的实现类
(2)User.java:
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
|
package
cn.zifangsky.entity;
import
javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
(name=
"userInfo"
)
public
class
User {
private
int
id;
private
String name;
private
String contact;
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id = id;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
String getContact() {
return
contact;
}
public
void
setContact(String contact) {
this
.contact = contact;
}
}
|
(3)RestService.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package
cn.zifangsky.service;
import
javax.ws.rs.GET;
import
javax.ws.rs.Path;
import
javax.ws.rs.PathParam;
import
javax.ws.rs.Produces;
import
javax.ws.rs.core.MediaType;
import
cn.zifangsky.entity.User;
public
interface
RestService {
@GET
@Path
(value=
"/user/{id}"
)
@Produces
(value = { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public
User getUser(
@PathParam
(
"id"
)
int
id);
}
|
(4)RestServiceImpl.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package
cn.zifangsky.service.impl;
import
cn.zifangsky.entity.User;
import
cn.zifangsky.service.RestService;
public
class
RestServiceImpl
implements
RestService {
public
User getUser(
int
id) {
User user =
new
User();
user.setId(id);
user.setName(
"zifangsky"
);
user.setContact(
"http://www.zifangsky.cn"
);
return
user;
}
}
|
(5)web.xml不变,修改service-beans.xml:
添加<jaxrs:server></jaxrs:server>节点,表示是RESTful服务
1
2
3
4
5
6
7
8
9
10
11
|
<
jaxrs:server
id
=
"userRest"
address
=
"/services/rest"
>
<
jaxrs:serviceBeans
>
<
bean
class
=
"cn.zifangsky.service.impl.RestServiceImpl"
/>
</
jaxrs:serviceBeans
>
<
jaxrs:extensionMappings
>
<
entry
key
=
"json"
value
=
"application/json"
/>
<
entry
key
=
"xml"
value
=
"application/xml"
/>
</
jaxrs:extensionMappings
>
</
jaxrs:server
>
|
添加之后,service-beans.xml这个文件的完整代码是:
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
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws
=
"http://cxf.apache.org/jaxws"
xmlns:jaxrs
=
"http://cxf.apache.org/jaxrs"
xmlns:soap
=
"http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd ">
<
bean
id
=
"outLoggingInterceptor"
class
=
"org.apache.cxf.interceptor.LoggingOutInterceptor"
/>
<
bean
id
=
"loggingFeature"
class
=
"org.apache.cxf.feature.LoggingFeature"
/>
<
bean
id
=
"inLoggingInterceptor"
class
=
"org.apache.cxf.interceptor.LoggingInInterceptor"
/>
<
jaxws:server
id
=
"sayHelloServices"
serviceClass
=
"cn.zifangsky.service.CXFService"
address
=
"/services/soap"
>
<
jaxws:serviceBean
>
<
bean
class
=
"cn.zifangsky.service.impl.CXFServiceImpl"
/>
</
jaxws:serviceBean
>
<
jaxws:outInterceptors
>
<
ref
bean
=
"outLoggingInterceptor"
/>
</
jaxws:outInterceptors
>
<
jaxws:inInterceptors
>
<
ref
bean
=
"inLoggingInterceptor"
/>
</
jaxws:inInterceptors
>
<
jaxws:features
>
<
ref
bean
=
"loggingFeature"
/>
<
wsa:addressing
xmlns:wsa
=
"http://cxf.apache.org/ws/addressing"
/>
</
jaxws:features
>
</
jaxws:server
>
<
jaxrs:server
id
=
"userRest"
address
=
"/services/rest"
>
<
jaxrs:serviceBeans
>
<
bean
class
=
"cn.zifangsky.service.impl.RestServiceImpl"
/>
</
jaxrs:serviceBeans
>
<
jaxrs:extensionMappings
>
<
entry
key
=
"json"
value
=
"application/json"
/>
<
entry
key
=
"xml"
value
=
"application/xml"
/>
</
jaxrs:extensionMappings
>
</
jaxrs:server
>
</
beans
>
|
(6)测试:
整个配置到这里已经就完成了,在tomcat中启动项目,效果是这样:
在浏览器中访问:http://localhost:8080/CXFDemo/services/rest/user/9
可以看出,结果是正确,到此全部配置结束
本文转自 pangfc 51CTO博客,原文链接:http://blog.51cto.com/983836259/1762762,如需转载请自行联系原作者