使用axis生成WebService调用客户端
本文中用到的服务端的内容,请移步链接:https://blog.youkuaiyun.com/Jack_David/article/details/86532983
查看具体的操作流程
第一步:新建Maven项目
1.在Eclipse中新建一个Maven项目,取名为StudClient,并配置pom.xml文件
2.将服务端提供的wsdl文件配置到项目的指定路径下面
新建的名为StudClient的Maven项目结构如下:
pom.xml文件配置如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.csdn</groupId>
<artifactId>StudClient</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>StudClient</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>axis</groupId>
<artifactId>axis-wsdl4j</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
</dependencies>
<build>
<finalName>StudClient</finalName>
</build>
</project>
服务端提供的StudentService.wsdl文件内容如下:
该wsdl文件,为上一个项目中自动生成的文件,由服务端提供即可
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://www.csdn.com/axis/StudentService" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://www.csdn.com/axis/StudentService" xmlns:intf="http://www.csdn.com/axis/StudentService" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)-->
<wsdl:message name="getStudentInfoResponse">
<wsdl:part name="getStudentInfoReturn" type="soapenc:string"/>
</wsdl:message>
<wsdl:message name="getStudentMarksRequest">
<wsdl:part name="studentNo" type="soapenc:string"/>
<wsdl:part name="subjectNo" type="soapenc:string"/>
</wsdl:message>
<wsdl:message name="getStudentInfoRequest">
<wsdl:part name="studentNo" type="soapenc:string"/>
</wsdl:message>
<wsdl:message name="getStudentMarksResponse">
<wsdl:part name="getStudentMarksReturn" type="soapenc:string"/>
</wsdl:message>
<wsdl:portType name="StudentServiceImpl">
<wsdl:operation name="getStudentInfo" parameterOrder="studentNo">
<wsdl:input message="impl:getStudentInfoRequest" name="getStudentInfoRequest"/>
<wsdl:output message="impl:getStudentInfoResponse" name="getStudentInfoResponse"/>
</wsdl:operation>
<wsdl:operation name="getStudentMarks" parameterOrder="studentNo subjectNo">
<wsdl:input message="impl:getStudentMarksRequest" name="getStudentMarksRequest"/>
<wsdl:output message="impl:getStudentMarksResponse" name="getStudentMarksResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="StudentServiceSoapBinding" type="impl:StudentServiceImpl">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getStudentInfo">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="getStudentInfoRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://www.csdn.com/axis/StudentService" use="encoded"/>
</wsdl:input>
<wsdl:output name="getStudentInfoResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://www.csdn.com/axis/StudentService" use="encoded"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getStudentMarks">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="getStudentMarksRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://www.csdn.com/axis/StudentService" use="encoded"/>
</wsdl:input>
<wsdl:output name="getStudentMarksResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://www.csdn.com/axis/StudentService" use="encoded"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="StudentServiceImplService">
<wsdl:port binding="impl:StudentServiceSoapBinding" name="StudentService">
<wsdlsoap:address location="http://127.0.0.1:8090/StudApp/services/StudentService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
第二步:Eclipse自动生成客户端代码
1.选中wsdl文件,右击,依次选择:Web Services>>Generate Client>>Next>>Finish即可生成客户端代码
2.生成后的项目目录结构如下:
以下代码供大家参考,是系统自动生成的,可以直接忽略不需要看
StudentServiceImpl类
/**
* StudentServiceImpl.java
*
* This file was auto-generated from WSDL
* by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
*/
package com.csdn.www.axis.StudentService;
public interface StudentServiceImpl extends java.rmi.Remote {
public java.lang.String getStudentInfo(java.lang.String studentNo)
throws java.rmi.RemoteException;
public java.lang.String getStudentMarks(java.lang.String studentNo,
java.lang.String subjectNo) throws java.rmi.RemoteException;
}
StudentServiceImplProxy类
package com.csdn.www.axis.StudentService;
public class StudentServiceImplProxy implements
com.csdn.www.axis.StudentService.StudentServiceImpl {
private String _endpoint = null;
private com.csdn.www.axis.StudentService.StudentServiceImpl studentServiceImpl = null;
public StudentServiceImplProxy() {
_initStudentServiceImplProxy();
}
public StudentServiceImplProxy(String endpoint) {
_endpoint = endpoint;
_initStudentServiceImplProxy();
}
private void _initStudentServiceImplProxy() {
try {
studentServiceImpl = (new com.csdn.www.axis.StudentService.StudentServiceImplServiceLocator())
.getStudentService();
if (studentServiceImpl != null) {
if (_endpoint != null)
((javax.xml.rpc.Stub) studentServiceImpl)
._setProperty(
"javax.xml.rpc.service.endpoint.address",
_endpoint);
else
_endpoint = (String) ((javax.xml.rpc.Stub) studentServiceImpl)
._getProperty("javax.xml.rpc.service.endpoint.address");
}
} catch (javax.xml.rpc.ServiceException serviceException) {
}
}
public String getEndpoint() {
return _endpoint;
}
public void setEndpoint(String endpoint) {
_endpoint = endpoint;
if (studentServiceImpl != null)
((javax.xml.rpc.Stub) studentServiceImpl)._setProperty(
"javax.xml.rpc.service.endpoint.address", _endpoint);
}
public com.csdn.www.axis.StudentService.StudentServiceImpl getStudentServiceImpl() {
if (studentServiceImpl == null)
_initStudentServiceImplProxy();
return studentServiceImpl;
}
public java.lang.String getStudentInfo(java.lang.String studentNo)
throws java.rmi.RemoteException {
if (studentServiceImpl == null)
_initStudentServiceImplProxy();
return studentServiceImpl.getStudentInfo(studentNo);
}
public java.lang.String getStudentMarks(java.lang.String studentNo,
java.lang.String subjectNo) throws java.rmi.RemoteException {
if (studentServiceImpl == null)
_initStudentServiceImplProxy();
return studentServiceImpl.getStudentMarks(studentNo, subjectNo);
}
}
StudentServiceImplService类
/**
* StudentServiceImplService.java
*
* This file was auto-generated from WSDL
* by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
*/
package com.csdn.www.axis.StudentService;
public interface StudentServiceImplService extends javax.xml.rpc.Service {
public java.lang.String getStudentServiceAddress();
public com.csdn.www.axis.StudentService.StudentServiceImpl getStudentService()
throws javax.xml.rpc.ServiceException;
public com.csdn.www.axis.StudentService.StudentServiceImpl getStudentService(
java.net.URL portAddress) throws javax.xml.rpc.ServiceException;
}
StudentServiceImplServiceLocator类
/**
* StudentServiceImplServiceLocator.java
*
* This file was auto-generated from WSDL
* by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
*/
package com.csdn.www.axis.StudentService;
public class StudentServiceImplServiceLocator extends
org.apache.axis.client.Service implements
com.csdn.www.axis.StudentService.StudentServiceImplService {
public StudentServiceImplServiceLocator() {
}
public StudentServiceImplServiceLocator(
org.apache.axis.EngineConfiguration config) {
super(config);
}
public StudentServiceImplServiceLocator(java.lang.String wsdlLoc,
javax.xml.namespace.QName sName)
throws javax.xml.rpc.ServiceException {
super(wsdlLoc, sName);
}
// Use to get a proxy class for StudentService
private java.lang.String StudentService_address = "http://127.0.0.1:8090/StudApp/services/StudentService";
public java.lang.String getStudentServiceAddress() {
return StudentService_address;
}
// The WSDD service name defaults to the port name.
private java.lang.String StudentServiceWSDDServiceName = "StudentService";
public java.lang.String getStudentServiceWSDDServiceName() {
return StudentServiceWSDDServiceName;
}
public void setStudentServiceWSDDServiceName(java.lang.String name) {
StudentServiceWSDDServiceName = name;
}
public com.csdn.www.axis.StudentService.StudentServiceImpl getStudentService()
throws javax.xml.rpc.ServiceException {
java.net.URL endpoint;
try {
endpoint = new java.net.URL(StudentService_address);
} catch (java.net.MalformedURLException e) {
throw new javax.xml.rpc.ServiceException(e);
}
return getStudentService(endpoint);
}
public com.csdn.www.axis.StudentService.StudentServiceImpl getStudentService(
java.net.URL portAddress) throws javax.xml.rpc.ServiceException {
try {
com.csdn.www.axis.StudentService.StudentServiceSoapBindingStub _stub = new com.csdn.www.axis.StudentService.StudentServiceSoapBindingStub(
portAddress, this);
_stub.setPortName(getStudentServiceWSDDServiceName());
return _stub;
} catch (org.apache.axis.AxisFault e) {
return null;
}
}
public void setStudentServiceEndpointAddress(java.lang.String address) {
StudentService_address = address;
}
/**
* For the given interface, get the stub implementation. If this service has
* no port for the given interface, then ServiceException is thrown.
*/
public java.rmi.Remote getPort(Class serviceEndpointInterface)
throws javax.xml.rpc.ServiceException {
try {
if (com.csdn.www.axis.StudentService.StudentServiceImpl.class
.isAssignableFrom(serviceEndpointInterface)) {
com.csdn.www.axis.StudentService.StudentServiceSoapBindingStub _stub = new com.csdn.www.axis.StudentService.StudentServiceSoapBindingStub(
new java.net.URL(StudentService_address), this);
_stub.setPortName(getStudentServiceWSDDServiceName());
return _stub;
}
} catch (java.lang.Throwable t) {
throw new javax.xml.rpc.ServiceException(t);
}
throw new javax.xml.rpc.ServiceException(
"There is no stub implementation for the interface: "
+ (serviceEndpointInterface == null ? "null"
: serviceEndpointInterface.getName()));
}
/**
* For the given interface, get the stub implementation. If this service has
* no port for the given interface, then ServiceException is thrown.
*/
public java.rmi.Remote getPort(javax.xml.namespace.QName portName,
Class serviceEndpointInterface)
throws javax.xml.rpc.ServiceException {
if (portName == null) {
return getPort(serviceEndpointInterface);
}
java.lang.String inputPortName = portName.getLocalPart();
if ("StudentService".equals(inputPortName)) {
return getStudentService();
} else {
java.rmi.Remote _stub = getPort(serviceEndpointInterface);
((org.apache.axis.client.Stub) _stub).setPortName(portName);
return _stub;
}
}
public javax.xml.namespace.QName getServiceName() {
return new javax.xml.namespace.QName(
"http://www.csdn.com/axis/StudentService",
"StudentServiceImplService");
}
private java.util.HashSet ports = null;
public java.util.Iterator getPorts() {
if (ports == null) {
ports = new java.util.HashSet();
ports.add(new javax.xml.namespace.QName(
"http://www.csdn.com/axis/StudentService", "StudentService"));
}
return ports.iterator();
}
/**
* Set the endpoint address for the specified port name.
*/
public void setEndpointAddress(java.lang.String portName,
java.lang.String address) throws javax.xml.rpc.ServiceException {
if ("StudentService".equals(portName)) {
setStudentServiceEndpointAddress(address);
} else { // Unknown Port Name
throw new javax.xml.rpc.ServiceException(
" Cannot set Endpoint Address for Unknown Port" + portName);
}
}
/**
* Set the endpoint address for the specified port name.
*/
public void setEndpointAddress(javax.xml.namespace.QName portName,
java.lang.String address) throws javax.xml.rpc.ServiceException {
setEndpointAddress(portName.getLocalPart(), address);
}
}
StudentServiceSoapBindingStub类
/**
* StudentServiceSoapBindingStub.java
*
* This file was auto-generated from WSDL
* by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
*/
package com.csdn.www.axis.StudentService;
public class StudentServiceSoapBindingStub extends org.apache.axis.client.Stub
implements com.csdn.www.axis.StudentService.StudentServiceImpl {
private java.util.Vector cachedSerClasses = new java.util.Vector();
private java.util.Vector cachedSerQNames = new java.util.Vector();
private java.util.Vector cachedSerFactories = new java.util.Vector();
private java.util.Vector cachedDeserFactories = new java.util.Vector();
static org.apache.axis.description.OperationDesc[] _operations;
static {
_operations = new org.apache.axis.description.OperationDesc[2];
_initOperationDesc1();
}
private static void _initOperationDesc1() {
org.apache.axis.description.OperationDesc oper;
org.apache.axis.description.ParameterDesc param;
oper = new org.apache.axis.description.OperationDesc();
oper.setName("getStudentInfo");
param = new org.apache.axis.description.ParameterDesc(
new javax.xml.namespace.QName("", "studentNo"),
org.apache.axis.description.ParameterDesc.IN,
new javax.xml.namespace.QName(
"http://schemas.xmlsoap.org/soap/encoding/", "string"),
java.lang.String.class, false, false);
oper.addParameter(param);
oper.setReturnType(new javax.xml.namespace.QName(
"http://schemas.xmlsoap.org/soap/encoding/", "string"));
oper.setReturnClass(java.lang.String.class);
oper.setReturnQName(new javax.xml.namespace.QName("",
"getStudentInfoReturn"));
oper.setStyle(org.apache.axis.constants.Style.RPC);
oper.setUse(org.apache.axis.constants.Use.ENCODED);
_operations[0] = oper;
oper = new org.apache.axis.description.OperationDesc();
oper.setName("getStudentMarks");
param = new org.apache.axis.description.ParameterDesc(
new javax.xml.namespace.QName("", "studentNo"),
org.apache.axis.description.ParameterDesc.IN,
new javax.xml.namespace.QName(
"http://schemas.xmlsoap.org/soap/encoding/", "string"),
java.lang.String.class, false, false);
oper.addParameter(param);
param = new org.apache.axis.description.ParameterDesc(
new javax.xml.namespace.QName("", "subjectNo"),
org.apache.axis.description.ParameterDesc.IN,
new javax.xml.namespace.QName(
"http://schemas.xmlsoap.org/soap/encoding/", "string"),
java.lang.String.class, false, false);
oper.addParameter(param);
oper.setReturnType(new javax.xml.namespace.QName(
"http://schemas.xmlsoap.org/soap/encoding/", "string"));
oper.setReturnClass(java.lang.String.class);
oper.setReturnQName(new javax.xml.namespace.QName("",
"getStudentMarksReturn"));
oper.setStyle(org.apache.axis.constants.Style.RPC);
oper.setUse(org.apache.axis.constants.Use.ENCODED);
_operations[1] = oper;
}
public StudentServiceSoapBindingStub() throws org.apache.axis.AxisFault {
this(null);
}
public StudentServiceSoapBindingStub(java.net.URL endpointURL,
javax.xml.rpc.Service service) throws org.apache.axis.AxisFault {
this(service);
super.cachedEndpoint = endpointURL;
}
public StudentServiceSoapBindingStub(javax.xml.rpc.Service service)
throws org.apache.axis.AxisFault {
if (service == null) {
super.service = new org.apache.axis.client.Service();
} else {
super.service = service;
}
((org.apache.axis.client.Service) super.service)
.setTypeMappingVersion("1.2");
}
protected org.apache.axis.client.Call createCall()
throws java.rmi.RemoteException {
try {
org.apache.axis.client.Call _call = super._createCall();
if (super.maintainSessionSet) {
_call.setMaintainSession(super.maintainSession);
}
if (super.cachedUsername != null) {
_call.setUsername(super.cachedUsername);
}
if (super.cachedPassword != null) {
_call.setPassword(super.cachedPassword);
}
if (super.cachedEndpoint != null) {
_call.setTargetEndpointAddress(super.cachedEndpoint);
}
if (super.cachedTimeout != null) {
_call.setTimeout(super.cachedTimeout);
}
if (super.cachedPortName != null) {
_call.setPortName(super.cachedPortName);
}
java.util.Enumeration keys = super.cachedProperties.keys();
while (keys.hasMoreElements()) {
java.lang.String key = (java.lang.String) keys.nextElement();
_call.setProperty(key, super.cachedProperties.get(key));
}
return _call;
} catch (java.lang.Throwable _t) {
throw new org.apache.axis.AxisFault(
"Failure trying to get the Call object", _t);
}
}
public java.lang.String getStudentInfo(java.lang.String studentNo)
throws java.rmi.RemoteException {
if (super.cachedEndpoint == null) {
throw new org.apache.axis.NoEndPointException();
}
org.apache.axis.client.Call _call = createCall();
_call.setOperation(_operations[0]);
_call.setUseSOAPAction(true);
_call.setSOAPActionURI("");
_call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
_call.setOperationName(new javax.xml.namespace.QName(
"http://www.csdn.com/axis/StudentService", "getStudentInfo"));
setRequestHeaders(_call);
setAttachments(_call);
try {
java.lang.Object _resp = _call
.invoke(new java.lang.Object[] { studentNo });
if (_resp instanceof java.rmi.RemoteException) {
throw (java.rmi.RemoteException) _resp;
} else {
extractAttachments(_call);
try {
return (java.lang.String) _resp;
} catch (java.lang.Exception _exception) {
return (java.lang.String) org.apache.axis.utils.JavaUtils
.convert(_resp, java.lang.String.class);
}
}
} catch (org.apache.axis.AxisFault axisFaultException) {
throw axisFaultException;
}
}
public java.lang.String getStudentMarks(java.lang.String studentNo,
java.lang.String subjectNo) throws java.rmi.RemoteException {
if (super.cachedEndpoint == null) {
throw new org.apache.axis.NoEndPointException();
}
org.apache.axis.client.Call _call = createCall();
_call.setOperation(_operations[1]);
_call.setUseSOAPAction(true);
_call.setSOAPActionURI("");
_call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
_call.setOperationName(new javax.xml.namespace.QName(
"http://www.csdn.com/axis/StudentService", "getStudentMarks"));
setRequestHeaders(_call);
setAttachments(_call);
try {
java.lang.Object _resp = _call.invoke(new java.lang.Object[] {
studentNo, subjectNo });
if (_resp instanceof java.rmi.RemoteException) {
throw (java.rmi.RemoteException) _resp;
} else {
extractAttachments(_call);
try {
return (java.lang.String) _resp;
} catch (java.lang.Exception _exception) {
return (java.lang.String) org.apache.axis.utils.JavaUtils
.convert(_resp, java.lang.String.class);
}
}
} catch (org.apache.axis.AxisFault axisFaultException) {
throw axisFaultException;
}
}
}
第三步:编写客户端的调用代码
3.1 编写代码
1.新建名为StudClient的客户端程序,项目结构如下:
StudClient类的代码如下:
package com.csdn.studclient;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import com.csdn.www.axis.StudentService.StudentServiceImpl;
import com.csdn.www.axis.StudentService.StudentServiceImplServiceLocator;
/**
*
* 获取学生信息的客户端调用类
*
* @author Jack_David
*
*/
public class StudClient {
public static void main(String[] args) {
StudentServiceImplServiceLocator locator = new StudentServiceImplServiceLocator();
try {
StudentServiceImpl client = locator.getStudentService();
String studentInfo = client.getStudentInfo("1002");// 根据学号查询学生基本信息
System.out.println("获取到的学生信息为:" + studentInfo);
String subjectInfo = client.getStudentMarks("1002", "8588");// 根据学号和学科号获取成绩
System.out.println("获取到的学生的学科信息为:" + subjectInfo);
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3.2 执行测试
1.调整StudentServiceImplServiceLocator类中的地址和端口,配置为指定的地址和端口,如下
private java.lang.String StudentService_address = "http://127.0.0.1:8090/StudApp/services/StudentService";
2.执行客户端测试程序,结果如下:
下图所示表示客户端程序调用成功