我在上一篇文章(http://www.zifangsky.cn/2016/03/apache-cxf實現(xiàn)的soap形式的webservices/)中說了下如何實現(xiàn)SOAP方式的web service,這篇文章將繼續(xù)說明如何實現(xiàn)RESTful形式的web service
創(chuàng)新互聯(lián)主營德保網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app軟件定制開發(fā),德保h5微信小程序開發(fā)搭建,德保網(wǎng)站營銷推廣歡迎德保等地區(qū)企業(yè)咨詢(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é)束
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。
網(wǎng)站標(biāo)題:ApacheCXF實現(xiàn)的RESTful形式的webservices-創(chuàng)新互聯(lián)
路徑分享:http://muchs.cn/article30/phspo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站設(shè)計公司、靜態(tài)網(wǎng)站、外貿(mào)建站、網(wǎng)站制作、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容