1、工程代码及配置
package com.webservice.cxf.server;
import javax.jws.WebService;
/**
* 天气查询服务接口
*
*/
@WebService
public interface WeatherInterface {
//天气查询
public String queryWeather(String cityName);
}
package com.webservice.cxf.server;
import javax.jws.WebService;
/**
* 天气查询服务
*
*/
@WebService(endpointInterface="com.webservice.cxf.server.WeatherInterface",serviceName="weather")
public class WeatherInterfaceImpl implements WeatherInterface {
@Override
public String queryWeather(String cityName) {
//接收客户端发送过来的数据
System.out.println("from client..." + cityName);
String result = "晴转阴";
//向客户端返回天气查询结果
return result;
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<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-2.5.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
xmlns="http://www.springframework.org/schema/beans">
<bean id="jaxWsServiceFactoryBean" class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
<property name="wrapped" value="true" />
</bean>
<jaxws:endpoint id="weatherInterfaceImpl" address="/weather"
implementor="com.webservice.cxf.server.WeatherInterfaceImpl">
<jaxws:serviceFactory>
<ref bean="jaxWsServiceFactoryBean" />
</jaxws:serviceFactory>
</jaxws:endpoint>
</beans>
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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>webservice_cxf_server</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<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>/ws/*</url-pattern>
</servlet-mapping>
</web-app>
2、启动web工程
启动tomcat。在浏览器里输入:http://localhost:8080/webservice_cxf_server/ws
3、安装cxf
在cxf官网下载cxf的3.1.8版本,笔者下载的zip版。将文件解压缩并放到一个合适的目录下。在环境变量中加入变量“CXF_HOME”,值就是安装路径;再在“PATH”变量中加入:
%CXF_HOME%\bin
4、使用“wsdl2java”命令生成接口类
wsdl2java的用法如下:
wsdl2java –p 包名 –d 目录名 wsdl路径
wsdl2java -frontend jaxws21 -p com.webservice.cxf.client.info -d D:\note http://localhost:8080/webservice1/hello/weather?wsdl
5、 创建客户端程序
创建一个web项目,将cxf的jar包都拷贝进去将服务器端生成的接口类拷贝过来,然后再创建一个测试类,内容如下:
package com.webservice.cxf.client.test;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.webservice.cxf.client.info.WeatherInterface;
public class ClientTest {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(WeatherInterface.class);
factory.setAddress("http://localhost:8080/webservice1/hello/weather");
WeatherInterface service = (WeatherInterface)factory.create();
String result = service.queryWeather("深圳");
System.out.println("from server..." + result);
}
}
参考博文:https://blog.csdn.net/flyingnet/article/details/79791345