專案例項:WebService axis1.4高階程式設計(服務端、客戶端)
阿新 • • 發佈:2019-01-04
package com.axis.test;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import com.axis.entity.Student;
import com.axis.entity.Teacher;
public class Client {
public static void main(String[] args) throws Exception{
//test1();
//test2();
//test3();
//test4();
//test5();
test6();
}
/**
* 測試doTestD服務介面
* @param 傳遞實體Bean(單個)
* @return 返回集合實體Bean(多個)
* @throws Exception
*/
public static void test1()throws Exception{
String wsdlAddress = "http://localhost:8082/axisTest/axisTest/myTestAxisWService?wsdl";//wsdl地址
Service service=new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(wsdlAddress);
QName inType = new QName("http://entity.axis.com", "Teacher");//第一個引數為空間名地址,第二個為實體序列/反序列名
call.registerTypeMapping(Teacher.class, inType, BeanSerializerFactory.class, BeanDeserializerFactory.class);
QName outType = new QName("http://entity.axis.com", "Student");
call.registerTypeMapping(Student.class, outType, BeanSerializerFactory.class, BeanDeserializerFactory.class);
call.setOperationName("doTestD");//呼叫的服務介面方法名
call.addParameter("teacher", XMLType.XSD_ANYTYPE, ParameterMode.IN);//傳遞的引數名(可隨便取)
call.setReturnClass(Student[].class);//返回的是集合Bean時,必須定義javaBean為陣列,如javaBean[]
Teacher teacher = new Teacher();
teacher.setAge(1);
teacher.setBirthday(new Date());
Student[] info = (Student[]) call.invoke(new Object[]{teacher});
System.out.println("--bean[]-"+info);
}
/**
* 測試doTestA服務介面
* @param 傳遞實體Bean(單個)
* @return 返回實體Bean(單個 )
* @throws Exception
*/
public static void test2()throws Exception{
String wsdlAddress = "http://localhost:8082/axisTest/axisTest/myTestAxisWService?wsdl";//wsdl地址
Service service=new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(wsdlAddress);
QName outType = new QName("http://entity.axis.com", "Student");
call.registerTypeMapping(Student.class, outType, BeanSerializerFactory.class, BeanDeserializerFactory.class);
call.setOperationName("doTestA");//呼叫的服務介面方法名
call.addParameter("name", XMLType.XSD_ANYTYPE, ParameterMode.IN);//傳遞的引數名(可隨便取)
call.setReturnClass(Student.class);//服務方法返回型別
Student info = (Student) call.invoke(new Object[]{"引數為單個字串"});
System.out.println("--bean-"+info);
}
/**
* 測試doTestF服務介面
* @param 傳遞物件Map
* @return 返回字串
* @throws Exception
*/
public static void test3()throws Exception{
String wsdlAddress = "http://localhost:8082/axisTest/axisTest/myTestAxisWService?wsdl";//wsdl地址
Service service=new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(wsdlAddress);
QName outType = new QName("http://entity.axis.com", "Student");
call.registerTypeMapping(Student.class, outType, BeanSerializerFactory.class, BeanDeserializerFactory.class);
call.setOperationName("doTestF");//呼叫的服務介面方法名
call.addParameter("paramMap", XMLType.XSD_ANYTYPE, ParameterMode.IN);//傳遞的引數名(可隨便取)
call.setReturnClass(String.class);//服務方法返回型別
Map<String, Student> paramMap = new HashMap<String, Student>();
Student sd = new Student();
sd.setAge(0);
paramMap.put("student1", sd);
String str = (String) call.invoke(new Object[]{paramMap});
System.out.println("--str-"+str);
}
/**
* 測試doTestC服務介面
* @param 傳遞物件Map
* @return 返回Map
* @throws Exception
*/
public static void test4()throws Exception{
String wsdlAddress = "http://localhost:8082/axisTest/axisTest/myTestAxisWService?wsdl";//wsdl地址
Service service=new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(wsdlAddress);
QName outType = new QName("http://entity.axis.com", "Student");
call.registerTypeMapping(Student.class, outType, BeanSerializerFactory.class, BeanDeserializerFactory.class);
call.setOperationName("doTestC");//呼叫的服務介面方法名
call.addParameter("paramMap", XMLType.XSD_ANYTYPE, ParameterMode.IN);//傳遞的引數名(可隨便取)
call.setReturnClass(Map.class);//服務方法返回型別
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("str", "隨便值");
Map<String, Student> str = (Map<String, Student>) call.invoke(new Object[]{paramMap});
System.out.println("--map-"+str);
}
/**
* 測試doTestE服務介面
* @param 傳遞物件List<Teacher>
* @throws Exception
*/
public static void test5()throws Exception{
String wsdlAddress = "http://localhost:8082/axisTest/axisTest/myTestAxisWService?wsdl";//wsdl地址
Service service=new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(wsdlAddress);
QName outType = new QName("http://entity.axis.com", "Teacher");
call.registerTypeMapping(Teacher.class, outType, BeanSerializerFactory.class, BeanDeserializerFactory.class);
call.setOperationName("doTestE");//呼叫的服務介面方法名
List<Teacher> infoList = new ArrayList<Teacher>();
Teacher info = new Teacher();
info.setAge(12);
infoList.add(info);
call.invoke(new Object[]{infoList});
System.out.println("-----無返回型別-----");
}
/**
* 測試doTestB服務介面
* @param 傳遞int and String
* @return 返回int
* @throws Exception
*/
public static void test6()throws Exception{
String wsdlAddress = "http://localhost:8082/axisTest/axisTest/myTestAxisWService?wsdl";//wsdl地址
Service service=new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(wsdlAddress);
call.setOperationName("doTestB");//呼叫的服務介面方法名
call.addParameter("a", XMLType.XSD_ANYTYPE, ParameterMode.IN);//傳遞的引數名(可隨便取)
call.addParameter("b", XMLType.XSD_ANYTYPE, ParameterMode.IN);//傳遞的引數名(可隨便取)
call.setReturnClass(int.class);//服務方法返回型別
int number = (Integer) call.invoke(new Object[]{111,"字串"});
System.out.println("--int-"+number);
}
}
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import com.axis.entity.Student;
import com.axis.entity.Teacher;
public class Client {
public static void main(String[] args) throws Exception{
//test1();
//test2();
//test3();
//test4();
//test5();
test6();
}
/**
* 測試doTestD服務介面
* @param 傳遞實體Bean(單個)
* @return 返回集合實體Bean(多個)
* @throws Exception
*/
public static void test1()throws Exception{
String wsdlAddress = "http://localhost:8082/axisTest/axisTest/myTestAxisWService?wsdl";//wsdl地址
Service service=new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(wsdlAddress);
QName inType = new QName("http://entity.axis.com", "Teacher");//第一個引數為空間名地址,第二個為實體序列/反序列名
call.registerTypeMapping(Teacher.class, inType, BeanSerializerFactory.class, BeanDeserializerFactory.class);
QName outType = new QName("http://entity.axis.com", "Student");
call.registerTypeMapping(Student.class, outType, BeanSerializerFactory.class, BeanDeserializerFactory.class);
call.setOperationName("doTestD");//呼叫的服務介面方法名
call.addParameter("teacher", XMLType.XSD_ANYTYPE, ParameterMode.IN);//傳遞的引數名(可隨便取)
call.setReturnClass(Student[].class);//返回的是集合Bean時,必須定義javaBean為陣列,如javaBean[]
Teacher teacher = new Teacher();
teacher.setAge(1);
teacher.setBirthday(new Date());
Student[] info = (Student[]) call.invoke(new Object[]{teacher});
System.out.println("--bean[]-"+info);
}
/**
* 測試doTestA服務介面
* @param 傳遞實體Bean(單個)
* @return 返回實體Bean(單個 )
* @throws Exception
*/
public static void test2()throws Exception{
String wsdlAddress = "http://localhost:8082/axisTest/axisTest/myTestAxisWService?wsdl";//wsdl地址
Service service=new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(wsdlAddress);
QName outType = new QName("http://entity.axis.com", "Student");
call.registerTypeMapping(Student.class, outType, BeanSerializerFactory.class, BeanDeserializerFactory.class);
call.setOperationName("doTestA");//呼叫的服務介面方法名
call.addParameter("name", XMLType.XSD_ANYTYPE, ParameterMode.IN);//傳遞的引數名(可隨便取)
call.setReturnClass(Student.class);//服務方法返回型別
Student info = (Student) call.invoke(new Object[]{"引數為單個字串"});
System.out.println("--bean-"+info);
}
/**
* 測試doTestF服務介面
* @param 傳遞物件Map
* @return 返回字串
* @throws Exception
*/
public static void test3()throws Exception{
String wsdlAddress = "http://localhost:8082/axisTest/axisTest/myTestAxisWService?wsdl";//wsdl地址
Service service=new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(wsdlAddress);
QName outType = new QName("http://entity.axis.com", "Student");
call.registerTypeMapping(Student.class, outType, BeanSerializerFactory.class, BeanDeserializerFactory.class);
call.setOperationName("doTestF");//呼叫的服務介面方法名
call.addParameter("paramMap", XMLType.XSD_ANYTYPE, ParameterMode.IN);//傳遞的引數名(可隨便取)
call.setReturnClass(String.class);//服務方法返回型別
Map<String, Student> paramMap = new HashMap<String, Student>();
Student sd = new Student();
sd.setAge(0);
paramMap.put("student1", sd);
String str = (String) call.invoke(new Object[]{paramMap});
System.out.println("--str-"+str);
}
/**
* 測試doTestC服務介面
* @param 傳遞物件Map
* @return 返回Map
* @throws Exception
*/
public static void test4()throws Exception{
String wsdlAddress = "http://localhost:8082/axisTest/axisTest/myTestAxisWService?wsdl";//wsdl地址
Service service=new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(wsdlAddress);
QName outType = new QName("http://entity.axis.com", "Student");
call.registerTypeMapping(Student.class, outType, BeanSerializerFactory.class, BeanDeserializerFactory.class);
call.setOperationName("doTestC");//呼叫的服務介面方法名
call.addParameter("paramMap", XMLType.XSD_ANYTYPE, ParameterMode.IN);//傳遞的引數名(可隨便取)
call.setReturnClass(Map.class);//服務方法返回型別
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("str", "隨便值");
Map<String, Student> str = (Map<String, Student>) call.invoke(new Object[]{paramMap});
System.out.println("--map-"+str);
}
/**
* 測試doTestE服務介面
* @param 傳遞物件List<Teacher>
* @throws Exception
*/
public static void test5()throws Exception{
String wsdlAddress = "http://localhost:8082/axisTest/axisTest/myTestAxisWService?wsdl";//wsdl地址
Service service=new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(wsdlAddress);
QName outType = new QName("http://entity.axis.com", "Teacher");
call.registerTypeMapping(Teacher.class, outType, BeanSerializerFactory.class, BeanDeserializerFactory.class);
call.setOperationName("doTestE");//呼叫的服務介面方法名
List<Teacher> infoList = new ArrayList<Teacher>();
Teacher info = new Teacher();
info.setAge(12);
infoList.add(info);
call.invoke(new Object[]{infoList});
System.out.println("-----無返回型別-----");
}
/**
* 測試doTestB服務介面
* @param 傳遞int and String
* @return 返回int
* @throws Exception
*/
public static void test6()throws Exception{
String wsdlAddress = "http://localhost:8082/axisTest/axisTest/myTestAxisWService?wsdl";//wsdl地址
Service service=new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(wsdlAddress);
call.setOperationName("doTestB");//呼叫的服務介面方法名
call.addParameter("a", XMLType.XSD_ANYTYPE, ParameterMode.IN);//傳遞的引數名(可隨便取)
call.addParameter("b", XMLType.XSD_ANYTYPE, ParameterMode.IN);//傳遞的引數名(可隨便取)
call.setReturnClass(int.class);//服務方法返回型別
int number = (Integer) call.invoke(new Object[]{111,"字串"});
System.out.println("--int-"+number);
}
}