一份Axis2 webService及动态客户段调运的例子
配置:最好copy Axis2下载包中的模板项目,然后添加所需的类库及修改配置文件。
Web.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>as</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:META-INF/spring/*.xml</param-value> </context-param>
<servlet> <servlet-name>AxisServlet</servlet-name>
<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
<load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>AxisAdminServlet</servlet-name> <servlet-class>org.apache.axis2.webapp.AxisAdminServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/servlet/AxisServlet</url-pattern> </servlet-mapping>
<servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>*.jws</url-pattern> </servlet-mapping>
<servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
<servlet-mapping> <servlet-name>AxisAdminServlet</servlet-name> <url-pattern>/axis2-admin/*</url-pattern> </servlet-mapping>
<mime-mapping> <extension>inc</extension> <mime-type>text/plain</mime-type> </mime-mapping>
<welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> <welcome-file>/axis2-web/index.jsp</welcome-file> </welcome-file-list>
<error-page> <error-code>404</error-code> <location>/axis2-web/Error/error404.jsp</location> </error-page>
<error-page> <error-code>500</error-code> <location>/axis2-web/Error/error500.jsp</location> </error-page> </web-app> |
WEB-INFO文件夹下面建立services文件夹
并在services建立一个文件夹名称任一,例如ws,在其中依次添加META-INF文件夹和services.xml,目录结构如下:
Services.xml配置,集成了spring:
<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
<service name="CustomerService"> <description> simple spring example </description> <parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier </parameter> <parameter name="SpringBeanName">CustomerService</parameter> <operation name="getCustomer">
<!-- <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" /> RPCMessageReceiver <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> -->
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>
<service name="ComplexTypeService"> <description> simple spring example </description> <parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier </parameter> <parameter name="SpringBeanName">ComplexTypeService</parameter>
<messageReceivers> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </messageReceivers>
<module ref="rampart" />
<parameter name="InflowSecurity"> <action> <items>UsernameToken Timestamp</items> <passwordCallbackClass>services.handler.PWDHandler </passwordCallbackClass> </action> </parameter>
</service>
</serviceGroup> |
Service代码:
package services;
import java.io.FileOutputStream;
public class ComplexTypeService { // 上传图像,imageByte参数表示上传图像文件的字节, // length参数表示图像文件的字节长度(该参数值可能小于imageByte的数组长度) public boolean uploadImageWithByte(byte[] imageByte,int length) { FileOutputStream fos = null; try { // 将上传的图像保存在D盘的test1.jpg文件中 fos = new FileOutputStream("d://test1.jpg"); // 开始写入图像文件的字节 fos.write(imageByte,length); fos.close(); } catch (Exception e) { return false; } finally { if (fos != null) { try { fos.close(); } catch (Exception e) {
} } } return true; }
// 返回一维字符串数组 public String[] getArray() { String[] strArray = new String[] { "自行车","飞机","火箭" }; return strArray; }
// 返回二维字符串数组 public String[] getMDArray() { String[] strArray = new String[] { "自行车,飞机,火箭","中国,美国,德国", "超人,蜘蛛侠,钢铁侠" }; return strArray; }
// 返回DataForm类的对象实例 public DataForm getDataForm() { return new DataForm(); }
// 将DataForm类的对象实例序列化,并返回序列化后的字节数组 public byte[] getDataFormBytes() throws Exception { java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(); java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(baos); oos.writeObject(new DataForm()); return baos.toByteArray(); } }
|
package services;
import java.io.Serializable;
public class DataForm implements Serializable {
private static final long serialVersionUID = 1455026627782545535L; private String name = "bill"; private int age = 20;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; } }
|
客户端调用:
客户端调用可以采用stub方式,用wsdl2java生成代码.
这儿是通用的动态调用方式,代码:
package axis2.client;
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class Axis2ClientUtilsForRPC { private RPCServiceClient serviceClient;
private String wsAddress; private String namespaceURI;
public Axis2ClientUtilsForRPC() {
}
public Axis2ClientUtilsForRPC(String wsAddress,String namespaceURI) { this.init(wsAddress,namespaceURI); }
public void init(String wsAddress,String namespaceURI) { this.setWsAddress(wsAddress); this.setNamespaceURI(namespaceURI); this.init(); }
public void init() { try {
serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(wsAddress); options.setTo(targetEPR); } catch (AxisFault e) {
e.printStackTrace(); } }
public Object[] invoke(String methodName,Object[] args,Class[] classes) { Object[] o = null; try { QName opAddEntry = new QName(namespaceURI,methodName); o = serviceClient.invokeBlocking(opAddEntry,args,classes); } catch (AxisFault e) {
e.printStackTrace(); } return o; }
public Object invokeForObject(String methodName, Class[] classes) { Object o = null; try { QName opAddEntry = new QName(namespaceURI,methodName); Object[] objects = serviceClient.invokeBlocking(opAddEntry, classes); if (objects != null && objects.length > 0) { o = objects[0]; } } catch (AxisFault e) {
e.printStackTrace(); } return o; }
public String getNamespaceURI() { return namespaceURI; }
public void setNamespaceURI(String namespaceURI) { this.namespaceURI = namespaceURI; }
public String getWsAddress() { return wsAddress; }
public void setWsAddress(String wsAddress) { this.wsAddress = wsAddress; }
}
|
测试代码:
package s1;
import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream; import java.util.Arrays;
import org.junit.Test;
import services.ComplexTypeServiceStub;
import axis2.client.Axis2ClientUtilsForRPC;
public class ComplexTypeServiceTest { Axis2ClientUtilsForRPC client = new Axis2ClientUtilsForRPC( "http://127.0.0.1:8080/as/services/ComplexTypeService", "http://services");
@Test public void testUploadImageWithByte() { Object[] opAddEntryArgs; Class[] classes; try {
// 打开图像文件,确定图像文件的大小 java.io.File file = new java.io.File("f://1.jpg"); java.io.FileInputStream fis = new java.io.FileInputStream(file); // 创建保存要上传的图像文件内容的字节数组 byte[] buffer = new byte[(int) file.length()]; // 将图像文件的内容读取buffer数组中 int n = fis.read(buffer); System.out.println("文件长度:" + file.length()); opAddEntryArgs = new Object[] { buffer,n }; classes = new Class[] { Boolean.class }; fis.close(); // 开始上传图像文件,并输出uploadImageWithByte方法的返回传 System.out.println(client.invoke("uploadImageWithByte", opAddEntryArgs,classes)[0]); } catch (FileNotFoundException e) {
e.printStackTrace(); } catch (IOException e) {
e.printStackTrace(); }
}
@Test public void testGetArray() { String[] v = (String[]) client.invokeForObject("getArray", new Object[] {},new Class[] { String[].class }); System.out.println(Arrays.asList(v)); }
@Test public void testGetMDArray() { String[] v = (String[]) client.invokeForObject("getMDArray",new Class[] { String[].class }); System.out.println(Arrays.asList(v)); }
@Test public void testGetDataForm() { Class[] returnTypes = new Class[] { ComplexTypeServiceStub.DataForm.class }; // 调用远程服务,得到返回的object数组 Object[] response = client.invoke("getDataForm",new Object[] {}, returnTypes); // 强制转换成 对象 ComplexTypeServiceStub.DataForm form = (ComplexTypeServiceStub.DataForm) response[0]; System.out.println("DataForm: name:" + form.getName() + ",age:" + form.getAge()); }
@Test public void testGetDataFormBytes() { try { byte[] v = (byte[]) client.invokeForObject("getDataFormBytes", new Object[] {},new Class[] { byte[].class });
ObjectInputStream o = new ObjectInputStream( new ByteArrayInputStream(v)); Object object = o.readObject(); services.DataForm form1 = (services.DataForm) object; System.out.println("DataForm: name:" + form1.getName() + ",age:" + form1.getAge()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }
}
}
package s1;
import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream; import java.rmi.RemoteException; import java.util.Arrays;
import javax.activation.DataHandler; import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient;
import services.ComplexTypeServiceStub; import services.ExceptionException0; import services.ComplexTypeServiceStub.DataForm; import services.ComplexTypeServiceStub.UploadImageWithByteResponse;
public class Main {
/** * @param args */ public static void main(String[] args) { c2(); }
private static void c1() { try { s1.CustomerServiceStub stub = new s1.CustomerServiceStub( "http://127.0.0.1:8080/as/services/CustomerService"); s1.CustomerServiceStub.GetCustomer c = new CustomerServiceStub.GetCustomer(); c.setCustId(111050l);
System.out.println(stub.getCustomer(c).get_return()); } catch (AxisFault e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } }
private static void c2() { try { ComplexTypeServiceStub stub = new ComplexTypeServiceStub(); // 调用一维数组 ComplexTypeServiceStub.GetArrayResponse res1 = stub.getArray(); System.out.println(Arrays.asList(res1.get_return())); // 调用二维数组 ComplexTypeServiceStub.GetMDArrayResponse res2 = stub.getMDArray(); System.out.println(Arrays.asList(res2.get_return())); // DataForm类的对象实例 ComplexTypeServiceStub.GetDataFormResponse res3 = stub .getDataForm(); DataForm form = res3.get_return(); System.out.println("DataForm: name:" + form.getName() + ",age:" + form.getAge()); // 将DataForm类的对象实例序列化,并返回序列化后的字节数组 ComplexTypeServiceStub.GetDataFormBytesResponse res4 = stub .getDataFormBytes(); DataHandler dh = res4.get_return(); try { ObjectInputStream o = new ObjectInputStream(dh.getInputStream()); Object object = o.readObject(); services.DataForm form1 = (services.DataForm) object; System.out.println("DataForm: name:" + form1.getName() + ",age:" + form1.getAge()); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 二进制图像 ComplexTypeServiceStub.UploadImageWithByte byte1 = new ComplexTypeServiceStub.UploadImageWithByte(); try { // 打开图像文件,确定图像文件的大小 java.io.File file = new java.io.File("f://1.jpg"); java.io.FileInputStream fis = new java.io.FileInputStream(file); // 创建保存要上传的图像文件内容的字节数组 byte[] buffer = new byte[(int) file.length()]; // 将图像文件的内容读取buffer数组中 int n = fis.read(buffer); System.out.println("文件长度:" + file.length()); Object[] opAddEntryArgs = new Object[] { buffer,n }; Class[] classes = new Class[] { Boolean.class };
fis.close();
UploadImageWithByteResponse res5 = stub .uploadImageWithByte(byte1); res5.get_return(); } catch (FileNotFoundException e) {
e.printStackTrace(); } catch (IOException e) {
e.printStackTrace(); }
} catch (AxisFault e) {
e.printStackTrace(); } catch (RemoteException e) {
e.printStackTrace(); } catch (ExceptionException0 e) { // TODO Auto-generated catch block e.printStackTrace(); } }
private static void c3() { try { RPCServiceClient client = new RPCServiceClient(); Options option = client.getOptions(); // 指定客户端访问的webservice服务器端地址 EndpointReference erf = new EndpointReference( "http://127.0.0.1:8080/as/services/ComplexTypeService"); option.setTo(erf); // 指定命名空间,指定要调用的方法 QName name = new QName("http://services","getDataForm"); // 创建返回的参数类型 Class[] returnTypes = new Class[] { ComplexTypeServiceStub.DataForm.class }; // 调用远程服务,得到返回的object数组 Object[] response = client.invokeBlocking(name, returnTypes); // 强制转换成 对象 ComplexTypeServiceStub.DataForm form = (ComplexTypeServiceStub.DataForm) response[0]; System.out.println("DataForm: name:" + form.getName() + ",age:" + form.getAge()); } catch (AxisFault e) { // TODO Auto-generated catch block e.printStackTrace(); }
}
private static void c4() { try { RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); EndpointReference targetEPR = new EndpointReference( "http://127.0.0.1:8080/as/services/ComplexTypeService"); options.setTo(targetEPR);
// 下面的代码调用uploadImageWithByte方法上传图像文件 // /////////////////////////////////////// // 打开图像文件,确定图像文件的大小 java.io.File file = new java.io.File("f://1.jpg"); java.io.FileInputStream fis = new java.io.FileInputStream(file); // 创建保存要上传的图像文件内容的字节数组 byte[] buffer = new byte[(int) file.length()]; // 将图像文件的内容读取buffer数组中 int n = fis.read(buffer); System.out.println("文件长度:" + file.length()); Object[] opAddEntryArgs = new Object[] { buffer,n }; Class[] classes = new Class[] { Boolean.class }; QName opAddEntry = new QName("http://services", "uploadImageWithByte"); fis.close(); // 开始上传图像文件,并输出uploadImageWithByte方法的返回传 System.out.println(serviceClient.invokeBlocking(opAddEntry,classes)[0]);
} catch (AxisFault e) {
e.printStackTrace(); } catch (FileNotFoundException e) {
e.printStackTrace(); } catch (IOException e) {
e.printStackTrace(); } } } |
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。