1、创建maven项目
2、打开pom文件,添加cxf依赖:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>apache-cxf</artifactId>
<version>3.1.10</version>
<type>pom</type>
</dependency>
3、编辑web-info下的web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>cxf</display-name>
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<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>/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</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"
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-servlet.xml"/>
<jaxws:endpoint id="hello" implementor="com.cxf.webservice.impl.HelloImpl" address="/HelloWorld"/>
</beans>
4、编写接口
package com.cxf.webservice;
import javax.jws.WebService;
@WebService
public interface Hello {
String sayHello();
}
package com.cxf.webservice.impl;
import com.cxf.webservice.Hello;
public class HelloImpl implements Hello{
@Override
public String sayHello() {
return " say hello!";
}
}
5、启动项目成功显示:
6、利用Apache cxf 的wsdl2java工具生成客户端访问类,Win+R进入{Apache cxf}\bin目录下执行wsdl2java http://localhost:8800/CxfDemo/HelloWorld?wsdl则会在bin目录下生成一个文件夹
拷贝到相应的目录下即可。
7、测试接口调用,在src/test/java下创建一个测试类如下:
import com.cxf.webservice.Hello;
import com.cxf.webservice.impl.HelloImplService;
public class HelloClient {
public static void main(String[] args) {
// TODO Auto-generated method stub
HelloImplService impl = new HelloImplService();
Hello hello = impl.getHelloImplPort();
System.out.println(hello.sayHello());
}
}
8、在服务端启动的情况下,执行run as ->java application
表示调用成功