開發cxf的web項目:
1、在MyEclipse中建立一個JavaWebProject,選擇使用Jdk1.6.0_24。
2、將cxf2.4中的lib目錄中的jar全部copy到WEB-INF/lib目錄下。
web.xml的配置如下:
<?xml version="1.0" encoding="UTF-8"?> <servlet> <!-- 配置cxf --> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <init-param> <!-- 配置Spring的配置文件 --> <param-name>config-location</param-name> <param-value>/WEB-INF/cxf-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
Cxf-servlet.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:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- 引入CXF Bean定義如下,早期的版本中使用 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- 直接發佈一個類,無接口 -->
<jaxws:endpoint id="one" implementor="cn.itcast.ws1.OneService"
address="/one">
</jaxws:endpoint>
<!-- 發佈一個服務,沒有指定接口 -->
<jaxws:server id="two" address="/two" serviceClass="cn.itcast.ws2.ITwoService">
<jaxws:serviceBean>
<!-- 指定發佈類,下面類必須添加@WebService註解 -->
<bean class="cn.itcast.ws2.TwoServiceImpl"></bean>
</jaxws:serviceBean>
</jaxws:server>
</beans>
1、通過MyEclipse發佈我們的項目。並在地址欄訪問http://localhost:9999/cxf2.4_spring_web/ws.應該出現以上的界面。
2、上面的程序是說,沒有發現任何已經發布的WebService,確實如此。請同學們重複上面的過程,看能否搭建一個cxf+spring環境出來。
3、接下來,我們將開始在此環境下,發佈我們的WebService.
配置發佈第一個ws : (jaxb)
<?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:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<!-- 通過jaxws發佈一個web服務。
implementor:實現者
address:地址
-->
<jaxws:endpoint
id="orderProcess"
implementor="a.OrderProcessImpl"
address="/order"></jaxws:endpoint>
<!-- 直接發佈一個沒有接口的類也是可以的 -->
<jaxws:endpoint
id="oneService"
implementor="b.OneService"
address="/one">
</jaxws:endpoint>
</beans>
發佈帶有接口的配置: (jax-ws)
<?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:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- 聲明方式說明:
serviceClass:必須爲一個接口,
並在接口上必須使用@WebService註解否則調用時會拋出異常
serviceBean:是實際服務的類,必須是serviceClass的子類
此類上面即可以使用@WebService註解,也可以不使用
address:訪問地址,省去前面的ip:port.
注意在此註冊的類,必須要添加@WebService的註解
-->
<jaxws:server id="one"
serviceClass="cn.one.IOneService"
address="/one">
<jaxws:serviceBean>
<bean class="cn.one.OneService"></bean>
</jaxws:serviceBean>
</jaxws:server>
</beans>
配置說明:
通過配置給服務添加消息攔截器:
<!-- 聲明方式說明:
serviceClass:必須爲一個接口,
並在接口上必須使用@WebService註解否則調用時會拋出異常
serviceBean:是實際服務的類,必須是serviceClass的子類
此類上面即可以使用@WebService註解,也可以不使用
address:訪問地址,省去前面的ip:port.
注意在此註冊的類,必須要添加@WebService的註解
-->
<jaxws:server id="one"
serviceClass="cn.one.IOneService"
address="/one">
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
<jaxws:serviceBean>
<bean class="cn.one.OneService"></bean>
</jaxws:serviceBean>
</jaxws:server>
在地址欄訪問:
1、點擊上面的超連接,將會看到發佈的wsdl文件。
2、至此,cxf和Spring的整合就算是完成了,是不是很簡單。下面我們將學習,如何在Spring的環境中訪問它。
Java項目代碼調用服務:
1、注意,此處所說的是在Java項目中調用Spring的服務,並不是在JavaEE項目中調用。後期將會講到如何在JavaEE項目中調用。
2、建議從wsdl地址獲取接口文件,也僅需要接口文件。
JaxWsProxyFactoryBeanclient =
new JaxWsProxyFactoryBean();
client.setAddress("http://localhost:7777/xcxf2_web/ws/one");
client.setServiceClass(IOneService.class);
IOneServiceone = client.create(IOneService.class);
Stringss= one.sayHi("OK你好");
System.err.println(ss);
在Spring項目中,通過配置文件調用:
建立好以後Java項目如下圖
1、說明:IHelloWorld.java是通過wsimport生成的接口,我們只需要這個接口。
2、CxfJavaClient.java包含main方法,用於通過ClientBeans.xml文件,調用WebService。
3、源代碼:
packagecom.itcast.cxf.first;
importjavax.jws.WebService;
/**
* 如果沒有改變包名的情況下,可以只使用@WebService註解。
* 如果修改了包名則應該使用
* @WebService(name="IHelloWorld",targetNamespace="http://wj.com")
* 所以,對於此類,仍然建議使用wsimport或wsdl2java生成。
* (僅需要此接口,其他類,不需要)
* @authorwangjianme
*
*/
@WebService
publicinterface IHelloWorld{
public String sayHello(Stringname);
}
ClientBeans.xml文件的內容:
說明:
通過<jaxws:client/>來獲取WebService,id就不用說了吧。
address是不包含?wsdl的服務地址。
serviceClass是本地的接口名,與服務接口名保持相同纔可以。
1、以下是ClientBeans.xml的文件的源代碼:
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="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:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/corehttp://cxf.apache.org/schemas/core.xsd">
<jaxws:clientid="helloClient"
address="http://127.0.0.1:9999/cxf2.4_spring_web/ws/helloworld"
serviceClass="com.itcast.cxf.first.IHelloWorld">
</jaxws:client>
</beans>
1、以下是CxfJavaClient.java的源代碼:
packagecom.itcast.cxfweb.java.client;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importcom.itcast.cxf.first.IHelloWorld;
/**
* Java項目的客戶端
* @authorwangjianme
*/
publicclass CxfJavaClient{
public static void main(String[] args){
//讀取配置文件
ApplicationContextctx=
new ClassPathXmlApplicationContext("ClientBeans.xml");
//get到接口類型並調用
IHelloWorldhello = (IHelloWorld)ctx.getBean("helloClient");
String str= hello.sayHello("WJ");
System.err.println(str);
}
}
關於web項目配置的說明1:
你好
<servlet>
<!--配置cxf-->
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<init-param>
<!--配置Spring的配置文件-->
<param-name>config-location</param-name>
<param-value>/WEB-INF/cxf-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
關於web項目配置的說明2:
<?xml version="1.0" encoding="UTF-8"?>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/cxf-itcast.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
關於web項目配置的說明3:
<!--關於cxf配置的注意事項
如果沒有提供給cxf默認的/WEB-INF/cxf-servlet.xml配置文件,則必須要在spring的配置文件
中配置以下三行配置。否則將不能加載名稱爲cxf的bean.從而啓動失敗。
-->
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<importresource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<importresource="classpath:META-INF/cxf/cxf-servlet.xml"/>
關於web項目配置的說明4:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* 獲取Spring的配置
*/
public class TT extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ApplicationContext ctx =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
Object o = ctx.getBean("one");
}
}