您好,登錄后才能下訂單哦!
我在上一篇文章(http://www.zifangsky.cn/2016/03/apache-cxf實現(xiàn)的soap形式的webservices/)中說了下如何實現(xiàn)SOAP方式的web service,這篇文章將繼續(xù)說明如何實現(xiàn)RESTful形式的web service
(1)項目結(jié)構(gòu):
在這里,我沒有新建一個項目,而是在上一個項目的基礎(chǔ)上實現(xiàn)的,具體來說就是新建了3個文件,如下圖所示:
其中,User類是一個普通的實體類,RestService和RestServiceImpl類分別表示服務(wù)接口和它的實現(xiàn)類
(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>節(jié)點,表示是RESTful服務(wù)
<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)測試:
整個配置到這里已經(jīng)就完成了,在tomcat中啟動項目,效果是這樣:
在瀏覽器中訪問:http://localhost:8080/CXFDemo/services/rest/user/9
可以看出,結(jié)果是正確,到此全部配置結(jié)束
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。