一、服务端
新建一个maven工程(打成war包)
pom依赖
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.10</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<!-- 指定端口 -->
<port>9090</port>
<!-- 请求路径 -->
<path>/cxf</path>
</configuration>
</plugin>
</plugins>
</build>
编写服务接口,@WebService 记得加!!
import javax.jws.WebService;
@WebService
public interface IWeatherService {
public String weatherInfo(String city);
}
实现类
public class WeatherServiceImpl implements IWeatherService{
public String weatherInfo(String city) {
if("北京".equals(city)) {
return "下雪";
}else {
return "下雨";
}
}
}
编写applicationContext-cxf.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">
<!-- WS调用的类 -->
<bean id="weatherService" class="com.zhida.service.impl.WeatherServiceImpl"></bean>
<!-- 发布服务 -->
<jaxws:server address="/weatherService">
<jaxws:serviceBean>
<ref bean="weatherService"/>
</jaxws:serviceBean>
</jaxws:server>
</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_2_5.xsd" version="2.5">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<filter>
<filter-name>cxf</filter-name>
<filter-class>org.apache.cxf.transport.servlet.CXFServlet</filter-class>
</filter>
<filter-mapping>
<filter-name>cxf</filter-name>
<url-pattern>/ws/*</url-pattern>
</filter-mapping>
</web-app>
测试运行:
tomcat7:run 浏览器输入:http://localhost:9090/cxf/ws/weatherService?wsdl
至此,服务端编写完成!
客户端
创建maven工程(jar)
pom依赖:
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.10</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
根据 WSDL 生成本地代码
命令行进入项目目录src/main/java目录下
运行命令:wsimport -s . http://localhost:9090/cxf/ws/weatherService?wsdl
如图:
刷新项目,发现自动生成了代码:
接下来,编写配置文件applicationContext_cxf.xml
配置客户端,address为 服务端发布的描述语言地址;
serviceClass为 生成的代码中的那个接口类,名称与portType中的名称一致
<?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">
<!--客户端配置 -->
<jaxws:client id="weatherClient" address="http://localhost:9090/cxf/ws/weatherService?wsdl"
serviceClass="com.zhida.service.impl.IWeatherService">
</jaxws:client>
</beans>
编写测试代码调用远程服务:
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zhida.service.impl.IWeatherService;
public class CXFTest {
@Test
public void testCXF() {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext_cxf.xml");
IWeatherService weather = (IWeatherService)ac.getBean("weatherClient");
System.out.println(weather.weatherInfo("北京"));
System.out.println(weather.weatherInfo("杭州"));
}
}
结果:
大功告成!
注意service端要保持运行状态!
附:WSDL介绍