引入spring和cxf相关jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<!--因为需要测试 所以用到junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- 需要添加Spring相关最少jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<!-- 添加CXF相关jar包 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.1.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.5</version>
</dependency>
打开apache-cxf-3.2.1下的bin 目录
输入cmd执行命令 :wsdl2java -p com.mr.service -client wsdl路径
执行成功生成文件
将生成的java文件导入到客户端指定目录下去
然后写一个测试类去测试是否可以调用成功
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:spring-cxf.xml")
public class Demo {
@Autowired
private Hello hello;
@Test
public void test1(){
String s= hello.save("111", 22, 1);
System.out.println(s);
}
}
在Spring配置文件中添加<jaxws:client id="" serviceClass="" address="" ></jaxws:client>标签
id:代表接口的名称
serviceClass:接口在客户端的全路径
address:访问的wsdl路径
<?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="hello" serviceClass="com.mr.cxf.client.Hello" address="http://localhost:8081/webservice/user?wsdl" ></jaxws:client>
</beans>
通过demo中的方法进行测试,成功后返回结果。