使用apache cxf实现webservice服务

1、在idea中使用maven新建web工程并引入spring mvc,具体可以参考https://www.cnblogs.com/laoxia/p/9311442.html;

2、在工程POM文件中引入apache cxf的相关jar包:

 <dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>apache-cxf</artifactId>
<version>3.2.4</version>
<type>pom</type>
</dependency>

3、定义webservice服务的接口:

 @WebService
public interface HelloWorld {
@WebMethod
String say(@WebParam(name = "inputText") String text);
}

4、实现webservice服务:

 @WebService
public class HelloWorldImpl implements HelloWorld { @Override
public String say(@WebParam(name = "inputText") String text) {
return "hello, My Test worlld: " + text;
}
}

5、定义发布webservice服务的xml文件cxfWebService.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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
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="HelloWorldBean" address="/helloworld" implementor="com.ucar.test.cxf.impl.HelloWorldImpl"/>
</beans>

6、在web.xml文件中指定加载webservice服务配置文件和CXFServlet:

 <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/cxfWebService.xml</param-value>
</context-param>
..................
<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>/webservice/*</url-pattern>
</servlet-mapping>

7、将工程打包成war包并放到tomcat下运行,在浏览器中打开http://localhost:8080/test11/webservice/helloworld?wsdl  即可看到下面的界面:

使用apache cxf实现webservice服务

此时可以编写客户端程序调用webservice服务,也可以使用wsCaller.jar来模拟调用webService服务,调用命令:java -jar wsCaller.jar;

上一篇:Kafka项目实战-用户日志上报实时统计之编码实践


下一篇:ElasticSearch实战-编码实践