版本:apache-cxf-3.2.1 JDK版本:1.8.0_25 注:此版本最低支持1.8 , JDK7 请下载 CXF 3.1.14 版本
- 必须引用的jar包列表 对应maven 依赖:
org.apache.cxf cxf-core 3.2.1 org.apache.cxf cxf-rt-bindings-soap 3.2.1 org.apache.cxf cxf-rt-databinding-jaxb 3.2.1 org.apache.cxf cxf-rt-frontend-jaxws 3.2.1 org.apache.cxf cxf-rt-frontend-simple 3.2.1 org.apache.cxf cxf-rt-transports-http 3.2.1 org.apache.cxf cxf-rt-wsdl 3.2.1 org.apache.neethi neethi 3.1.0 org.codehaus.woodstox stax2-api 3.1.4 org.codehaus.woodstox woodstox-core-asl 4.4.1 wsdl4j wsdl4j 1.6.3 org.apache.ws.xmlschema xmlschema-core 2.2.2 - 修改web.xml文件,引入代码片段
Apache CXF Endpoint cxf cxf org.apache.cxf.transport.servlet.CXFServlet 1 cxf /services/* - 创建接口与实现类 HelloWord.java
package com.quickmap.common;import javax.jws.WebParam;import javax.jws.WebService;// START SNIPPET: service@WebServicepublic interface HelloWorld { String sayHi(@WebParam(name="text")String text);// @WebParam 里面的name是请求时候的参数名}// END SNIPPET: service
-
HelloWorldImpl.java
// START SNIPPET: servicepackage com.quickmap.common;import javax.jws.WebService;@WebService(endpointInterface = "com.quickmap.common.HelloWorld", serviceName = "HelloWorld")public class HelloWorldImpl implements HelloWorld { public String sayHi(String text) { System.out.println("sayHi called"); return "Hello " + text; }}// END SNIPPET: service
- 创建注入配置文件cxf-servlet.xml
- 启动tomcat 看到以下输出信息,说明发布成功
- 在浏览器访问该地址 http://localhost:8080/nkydsj/services/hello_world?wsdl
- 客户端调用代码 Client.java运行后
package com.quickmap.common;import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;public final class Client { public static void main(String args[]) throws Exception { JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance(); org.apache.cxf.endpoint.Client client = clientFactory.createClient("http://localhost:8080/nkydsj/services/hello_world?wsdl"); Object[] result = new Object[0]; try { result = client.invoke("sayHi", ",webservice调用成功啦!!!"); System.out.println(result[0]); } catch (Exception e) { e.printStackTrace(); } }}