您好,登錄后才能下訂單哦!
我在上一篇文章(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
可以看出,結果是正確,到此全部配置結束
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。