在這小例子里我就不多做開發環境之類的說明了(上一例子已經交代清楚了),所以接下來就直接開打了!
新建一個web項目,命名為:CXFServer
建一個包:com.server.dao
在該包下面建一個接口,命名為:Hello,具體代碼如下:
package com.server.dao;
import javax.jws.WebService;
@WebService
public interface Hello {
public String hello(String username);
}
新建一個包:com.server.service
在該包下建一個現實類,命名為:HelloImpl具體代碼如下:
package com.server.service;
import javax.jws.WebService;
import com.server.dao.Hello;
@WebService
public class HelloImpl implements Hello {
public String hello(String username) {
System.out.println("server is called!");
return "sayHello" + username;
}
}
既然要用到Spring,那么就少不了在web.xml里面配置Spring的過濾器!
在web.xml配置Spring,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>apache cxf 配置 webservice 服務</description>
<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>/services/*</url-pattern>
</servlet-mapping>
<listener>
<description>spring 的監聽</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<description>spring 的配置文件加載路徑</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
</web-app>
項目增加Spring功能后,那么就要配置Spring文件了!
Spring配置文件如下;
<?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"
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">
<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="service"
implementor="com.server.service.HelloImpl" address="/webserviceHello" />
</beans>
<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" />
這3句是固定的一個配置!
<jaxws:endpoint id="service"
implementor="com.server.service.HelloImpl" address="/webserviceHello" />
id:指在spring配置的bean的ID.
Implementor:指明具體的實現類.
Address:指明這個web service的相對地址
把項目發布到tomcat上,啟動tomcat,在瀏覽器打開http://localhost:8080/CXFServer/services/webserviceHello?wsdl 能現實如下界面,證明服務器已經成功發布了!
有的同學可能會對這個訪問地址存在疑問:
http://localhost:8080/CXFServer/services/webserviceHello?wsdl
問什么是這樣子的訪問地址呢?
http://localhost:8080/CXFServer是本項目的訪問地址
services是由于web.xml配置所得:
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
webserviceHello是由于Spring配置文件中的address屬性所得:
<jaxws:endpoint id="service"
implementor="com.server.service.HelloImpl" address="/webserviceHello" />
現在服務器發布成功了,接著就寫客戶端程序了!
新建一個web項目,命名為;CXFClient
建一個包;com.server.dao(這個接口的包名要與服務器接口包名一樣)
建一個接口:Hello(最好與服務器的接口名字一樣)
代碼如下:
package com.server.dao;
import javax.jws.WebService;
@WebService
public interface Hello {
//這里的方法名必須與服務器接口的方法一樣
public String hello(String username);
}
新建一個測試類:Test
代碼如下:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext*.xml");
Hello service = (Hello) context.getBean("webServiceClient");
System.out.println(service.hello("和諧dota"));
}
}
代碼基本上完成了,現在為項目增加Spring功能,web.xml配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>apache cxf 配置 webservice 服務</description>
<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>/services/*</url-pattern>
</servlet-mapping>
<listener>
<description>spring 的監聽</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<description>spring 的配置文件加載路徑</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
</web-app>
對應的Spring配置文件如下所示:
<?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"
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">
<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:client id="webServiceClient"
address="http://localhost:8080/CXFServer/services/webserviceHello"
serviceClass="com.server.dao.Hello" />
</beans>
最后執行測試類:Test的代碼,控制臺會打印出:sayHello和諧dota
到此,CXF與Spring整合已經完成了!!希望能給你帶來一點幫助!!
注解:Spring配置文件放在src目錄下就可以了!
pasting