WebService:CXF釋出Web專案
在web.xml中配置
<?xmlversion="1.0" encoding="UTF-8"?>
<web-appversion="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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</
</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.xm
<?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">
<!-- 引入CXFBean定義如下,早期的版本中使用,
匯入的三個配置檔案都在cxf.jar的META資料夾中
--><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:endpointid="one" implementor="cn.itcast.ws1.OneService"
address="/one">
</jaxws:endpoint>
<!-- 釋出一個服務,沒有指定介面 --
<!-- 宣告方式說明:
serviceClass:必須為一個介面,
並在介面上必須使用@WebService註解否則呼叫時會丟擲異常
serviceBean:是實際服務的類,必須是serviceClass的子類
此類上面即可以使用@WebService註解,也可以不使用
address:訪問地址,省去前面的ip:port.
注意在此註冊的類,必須要新增@WebService的註解
-->
>
<jaxws:serverid="two" address="/two" serviceClass="cn.itcast.ws2.ITwoService">
<jaxws:serviceBean>
<!-- 指定釋出類,下面類必須新增@WebService註解 -->
<beanclass="cn.itcast.ws2.TwoServiceImpl"></bean>
</jaxws:serviceBean>
</jaxws:server>
</beans>
l lCXFServlet類,通過讀取config-location的配置項讀取cxf-servlet.xml配置檔案。並在內部讀取自己的配置檔案cxf.xml檔案。 l在CXFServlet讀取配置檔案後,將檔案資訊全部讀取到ApplicationContext的Spring類中。 以下是它的原始碼:
新增 攔截器
<!-- 宣告方式說明:
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>
客戶端呼叫
l使用純Java專案呼叫 •1、根據客戶端生成的程式碼來呼叫。(優選這種方式) •請先生成然後在任意的Java專案中呼叫 。 •2、客戶端只擁有一個介面,使用JaxWsProxyFactoryBean來呼叫。 •因為以下使用了JaxWsProxyFactoryBean,所以,仍然需要CXF的環境,而使用此環境就會造成Jar檔案的大量冗餘,所以大家要謹慎選擇。JaxWsProxyFactoryBean client =
newJaxWsProxyFactoryBean();
client.setAddress("http://localhost:7777/xcxf2_web/ws/one");
client.setServiceClass(IOneService.class);
IOneService one = client.create(IOneService.class);
String ss = one.sayHi("OK你好");
System.err.println(ss);
在Spring專案中,通過配置檔案呼叫
1、說明:IHelloWorld.java是通過wsimport生成的介面,我們只需要這個介面。
2、CxfJavaClient.java包含main方法,用於通過ClientBeans.xml檔案,呼叫WebService。
3、原始碼:
package com.itcast.cxf.first;
import javax.jws.WebService;
/**
* 如果沒有改變包名的情況下,可以只使用@WebService註解。
* 如果修改了包名則應該使用
* @WebService(name="IHelloWorld",targetNamespace="http://wj.com")
* 所以,對於此類,仍然建議使用wsimport或wsdl2java生成。
* (僅需要此介面,其他類,不需要)
* @author wangjianme
*
*/
@WebService
publicinterface IHelloWorld {
public String sayHello(Stringname);
}
以下是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:client id="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的原始碼:
package com.itcast.cxfweb.java.client;
import org.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
import com.itcast.cxf.first.IHelloWorld;
/**
* Java專案的客戶端
* @author wangjianme
*/
publicclass CxfJavaClient {
public static void main(String[] args) {
//讀取配置檔案
ApplicationContextctx =
new ClassPathXmlApplicationContext("ClientBeans.xml");
//get到介面型別並呼叫
IHelloWorld hello =(IHelloWorld)ctx.getBean("helloClient");
String str = hello.sayHello("WJ");
System.err.println(str);
}
}