// UserAction.java import java.io.FileOutputStream; import java.io.IOException; import java.util.Random; import javax.xml.stream.XMLStreamException; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; /** * pojo class * 服务端信息 * @author dycc */ public class UserAction { private static String namespace = "http://ws.apache.org/axis2"; /** * 有返回值:无参 * 服务器当前版本 * @return */ public String version(){ return "版本:1.0"; } /** * 有返回值:有简单参数 * 随机数 * @param from * @param to * @return */ public int randomNumber(int from,int to){ Random random = new Random(); int range = to - from; if(range < 1){ range = 1; } int num = random.nextInt(range) + from; return num; } /** * 有返回值:有复杂参数 * 用户注册 * @param userId * @return */ public boolean register(UserInfo user){ if(user == null || user.getId().equals("")){ System.out.println("false-" + user); return false; } // 查看用户信息 System.out.println("true-" + user); return true; } /** * 无返回值:有简单参数 * 注销 * @param userId */ public void logOut(String userId){ System.out.println("下线:" + userId); } /** * 有返回值:有复杂参数 * 上传图像 * @param imageByte 图像文件字节数组 * @param length 图像文件字节长度 <= imageByte.length * @return */ public boolean uploadImageWithByte(byte[] imageByte,int length){ String path = "D:/Temp/uu.jpg"; FileOutputStream fos = null; try{ fos = new FileOutputStream(path); fos.write(imageByte,0,length); fos.flush(); fos.close(); } catch(Exception e){ return false; } finally{ if(fos != null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; } /** * 有返回值:有参 * @param element * @return * @throws XMLStreamException */ public OMElement bookInfo(OMElement element)throws XMLStreamException{ element.build(); element.detach(); // String isbn = element.getText(); // OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace( namespace, // uri ""); // prefix // OMElement method = fac.createOMElement("bookInfoResponse",omNs); OMElement om_isbn = fac.createOMElement("isbn",omNs); OMElement om_name = fac.createOMElement("bookName",omNs); OMElement om_pub = fac.createOMElement("publisher",omNs); // om_isbn.addChild(fac.createOMText(om_isbn,"isbn:"+isbn)); om_name.addChild(fac.createOMText(om_name,"bookName:编程之美")); om_pub.addChild(fac.createOMText(om_pub,"出版社:电子工业出版社")); method.addChild(om_isbn); method.addChild(om_name); method.addChild(om_pub); // return method; } } // UserInfo.java import java.io.Serializable; /** * 用户信息 * @author dycc * */ public class UserInfo implements Serializable{ /** * */ private static final long serialVersionUID = 1L; // 用户 id private String id = ""; // 用户名 private String name = ""; // 级别 private int level = 0; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } /** * toString */ public String toString(){ StringBuffer sb = new StringBuffer(); sb.append("UserInfo:["); sb.append("id=" + id); sb.append(",name=" + name); sb.append(",level=" + level); sb.append("]"); return sb.toString(); } } // SimRpcClient.java import java.io.File; import java.io.FileInputStream; import java.util.Iterator; import javax.xml.namespace.QName; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.axis2.Constants; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; public class SimRpcClient { private static String toEpr = "http://localhost:8080/ws/services/UserAction/"; private static String namespace = "http://ws.apache.org/axis2"; private static RPCServiceClient client = null; static { try{ client = new RPCServiceClient(); }catch(Exception e){ e.printStackTrace(); } } /** * @param args */ public static void main(String[] args) { SimRpcClient t = new SimRpcClient(); // t.invokeReturnNoArgs(); // 有返回值:无参 t.invokeReturnSimpleArgs(); // 有返回值:有简单参数 t.invokeReturnComplexArgs(); // 有返回值:有复杂参数 [一个对象实例] t.invokeSimpleArgs(); // 无返回值:有简单参数 t.invokeReturnComplexArgs2(); // 有返回值:有复杂参数[byte数组]上传图片 t.invokeIom(); // 有返回值:有复杂参数[OMElement] } /** * 有返回值:无参 */ public void invokeReturnNoArgs(){ try{ Options options = client.getOptions(); options.setTo(new EndpointReference(toEpr)); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); // Object[] args = new Object[]{}; // 无参 Class[] classes = new Class[]{String.class}; // 返回类型: String QName opName = new QName(namespace,"version"); // String result = (String)client.invokeBlocking(opName,args,classes)[0]; // System.out.println(result); }catch(Exception e){ e.printStackTrace(); } } /** * 有返回值:有简单参数 */ public void invokeReturnSimpleArgs(){ try{ Options options = client.getOptions(); options.setTo(new EndpointReference(toEpr)); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); // Object[] args = new Object[]{10,22}; // 简单参数 Class[] classes = new Class[]{int.class}; // 返回类型: int QName opName = new QName(namespace,"randomNumber"); // int result = (Integer)client.invokeBlocking(opName,args,classes)[0]; // System.out.println(result); }catch(Exception e){ e.printStackTrace(); } } /** * 有返回值:有复杂参数 [一个对象实例] */ public void invokeReturnComplexArgs(){ try{ Options options = client.getOptions(); options.setTo(new EndpointReference(toEpr)); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); // UserInfo user = new UserInfo(); user.setId("stu_007"); user.setName("xuanyuan"); user.setLevel(10); Object[] args = new Object[]{user}; // 复杂参数 Class[] classes = new Class[]{boolean.class}; // 返回类型: boolean QName opName = new QName(namespace,"register"); // boolean result = (Boolean)client.invokeBlocking(opName,args,classes)[0]; // System.out.println(result); }catch(Exception e){ e.printStackTrace(); } } /** * 无返回值:有简单参数 */ public void invokeSimpleArgs(){ try{ Options options = client.getOptions(); options.setTo(new EndpointReference(toEpr)); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); // Object[] args = new Object[]{"明月"}; // 简单参数 QName opName = new QName(namespace,"logOut"); // client.invokeRobust(opName, args); // 调用无返回值的操作 }catch(Exception e){ e.printStackTrace(); } } /** * 有返回值:有复杂参数[byte数组]上传图片 */ public void invokeReturnComplexArgs2(){ try { Options options = new Options(); options.setTo(new EndpointReference(toEpr)); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); // String path = "E:\\pictures\\02.jpg"; File file = new File(path); FileInputStream in = new FileInputStream(file); byte[] buf = new byte[(int) file.length()]; // 数组的大小只能为int,不能为long int len = in.read(buf); in.close(); Object[] args = new Object[] { buf, len }; // 复杂参数 Class[] classes = new Class[] { boolean.class };// 返回类型:boolean QName opName = new QName(namespace, "uploadImageWithByte"); boolean res = (Boolean)client.invokeBlocking(opName, args, classes)[0]; System.out.println("上传图片:" + res); }catch(Exception e){ e.printStackTrace(); } } /** * 有返回值:有复杂参数[OMElement] */ public void invokeIom(){ try{ Options options = new Options(); options.setTo(new EndpointReference(toEpr)); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); // OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace( namespace, // uri ""); // prefix OMElement method = fac.createOMElement("bookInfo",omNs); OMElement value = fac.createOMElement("isbn",omNs); value.addChild(fac.createOMText(value,"978-7-121-06074-8中文")); method.addChild(value); // OMElement result = client.sendReceive(method); // OMElement returnEl = result.getFirstElement(); OMElement responseEl = returnEl.getFirstElement(); Iterator<OMElement> it = responseEl.getChildElements(); while(it.hasNext()){ OMElement el = it.next(); System.out.println(el.getText()); } // System.out.println("...end..."); }catch(Exception e){ e.printStackTrace(); } } }