需要的jar
<dependency> <groupId>org.apache.axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>javax.xml.rpc</groupId> <artifactId>javax.xml.rpc-api</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>commons-discovery</groupId> <artifactId>commons-discovery</artifactId> <version>0.2</version> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.6.2</version> </dependency>
java 代码
import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; public static void main(String[] args) { try { String url = "http://localhost:9000/HelloWorld?wsdl"; //直接引用远程的wsdl文件 //以下都是套路 Service service = new Service(); Call call = (Call)service.createCall(); call.setTargetEndpointAddress(url); call.setOperationName(new QName("http://example/","sayHelloWorldFrom"));//命名空间url 和方法名字 // call.addParameter("from", org.apache.axis.encoding.XMLType.XSD_STRING, // javax.xml.rpc.ParameterMode.IN);//接口的参数 // 参数名, 参数类型String, IN 或 OUT call.addParameter("arg0", XMLType.XSD_STRING, ParameterMode.IN); // 这里参数不能写参数名, arg0 代表第一个参数 call.setReturnType(XMLType.XSD_STRING);//设置返回类型 String result = (String)call.invoke(new Object[]{"xxx"}); //给方法传递参数,并且调用方法 System.out.println("result is "+result); } catch (Exception e) { System.err.println(e.toString()); } }