首先使用java wsimport命令或者 使用cxf提供的命令下载服务接口代码
java命令方式 https://blog.csdn.net/kxj19980524/article/details/84673457
这个是基于cxf发布服务 https://blog.csdn.net/kxj19980524/article/details/84675258
CXF方式,先下载好cxf ,然后进入bin目录打开黑窗口,然后输入命令
wsdl2java -d . -p com.bgs.client 后面写服务地址
然后只需要复制一个接口到项目当中去就可以了,然后导入jar包,导入配置文件
下面是cxf.xml 配置文件 里面的路径就是服务端的路径把后面的?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"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- 注册CXF客户端代理对象,通过spring框架创建这个对象,使用代理对象实现远程调用 -->
<jaxws:client id="myClient" address="http://192.168.1.4:8080/cxf-service/service/cxfService" serviceClass="com.bgs.client.HelloService">
</jaxws:client>
</beans>
然后写java类生成代理对象调用,就调用成功了
package com.bgs.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("cxf.xml");
HelloService bean = (HelloService) ctx.getBean("myClient");
String sayHello = bean.sayHello("test");
System.out.println(sayHello);
}
}