我在上一篇文章(http://www.zifangsky.cn/2016/03/apache-cxf实现的soap形式的webservices/)中说了下如何实现SOAP方式的web service,这篇文章将继续说明如何实现RESTful形式的web service
(1)项目结构:
在这里,我没有新建一个项目,而是在上一个项目的基础上实现的,具体来说就是新建了3个文件,如下图所示:
其中,User类是一个普通的实体类,RestService和RestServiceImpl类分别表示服务接口和它的实现类
(2)User.java:
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:
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:
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服务
<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这个文件的完整代码是:
<?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
可以看出,结果是正确,到此全部配置结束
时间: 2024-10-30 15:56:16