添加需要的依赖:
<!-- WebService 框架-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>19.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.2.5</version>
<scope>compile</scope>
</dependency>
实现方法:
//wsdlUrl wsdl地址
String wsdlUrl = "";
String message = ""
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = null;
try {
client = dcf.createClient(wsdlUrl);
System.out.println(client + "测试打印输出客户端----------------------------------------------------");
} catch (Exception e) {
e.printStackTrace();
}
if (client == null) {
throw new NullPointerException();
}
HTTPConduit conduit = null;
if (client.getConduit() != null) {
conduit = (HTTPConduit) client.getConduit();
}
System.out.println(client + "测试打印输出客户端----------------------------------------------------");
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(30000); //连接超时时间
policy.setReceiveTimeout(60000);//请求超时时间.
if (conduit == null) {
throw new NullPointerException();
}
conduit.setClient(policy);
Object[] o = new Object[2];
try {
o = client.invoke("Services", message);
}catch (Exception e){
System.out.println("++++++++++++++++"+e.getMessage());
}
for (Object o1 : o) {
System.out.println(o1 + "测试打印输出返回值----------------------------------------------------");
}
连接的时候发现一个问题,还没有找到原因,同一个定时任务调用两次这个方法,第二次会报错。只调用一次就很正常。想不通