1. 新建Dynamic Web project。
2. 从Apache的官网上下载CXF的jar包,地址:http://cxf.apache.org/download.html。将下载的包中的所有的jar包放在工程中。
3.编程:
服务器端:
package services; import javax.jws.WebService; @WebService public interface IHelloService { public String sayHello(String username); }
package services.impl; import javax.jws.WebService; import services.IHelloService; @WebService(endpointInterface = "services.IHelloService") public class HelloImpl implements IHelloService { @Override public String sayHello(String hello) { return "Hello, " + hello + "!"; } }
4. 在WEB-INF下创建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" 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="webServiceHello" address="/Hello" implementor="services.impl.HelloImpl"/> </beans>
5. 修改web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/beans.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app>
6. 将工程导出为war包,放入Tomcat的webapps文件夹下,运行tomcat。
7. 访问http://localhost:8080/cxfServer/services/Hello?wsdl可见服务已经启动。