按照下面,分别建2个工程,一个client(客户端),一个server(服务端)
先实现服务端:
1、编写services.xml文件,该文件是放在aar文件里的\META-INF目录下的:
<?xml version="1.0" encoding="UTF-8"?> <!-- This file was auto-generated from WSDL --> <!-- by the Apache Axis2 version: 1.4.1 Built on : Aug 19, 2008 (10:13:39 LKT) <parameter name="useOriginalwsdl">false</parameter> <parameter name="modifyUserWSDLPortAddress">true</parameter> --> <serviceGroup> <service name="axis2Service"> <description> This is a sample Web Service. </description> <!-- // ServiceClass指定Java Class的位置,即实现服务的类。 --> <parameter name="ServiceClass" locked="false">com.study.axis2.service.impl.Axis2ServiceImpl</parameter> <!-- // operation 与Java Class中方法名对应。 --> <operation name="user"> <!-- // messageReceiver看下文注解。 --> <messageReceiver class="com.study.axis2.receive.Axis2MessageReceiverInOut"/> </operation> </service> </serviceGroup>
2、编写Axis2ServiceImpl类:
package com.study.axis2.service.impl; import com.study.axis2.domain.User; import com.study.axis2.domain.UserResponse; import com.study.axis2.service.Axis2Service; public class Axis2ServiceImpl implements Axis2Service { public UserResponse user(User user) { // 将in转换为String。 int userId = user.getUserId(); String userName = user.getUserName(); System.out.println("USER ID : " + userId + "; USER NAME : " + userName); UserResponse response = new UserResponse(); response.setRspCode("0000"); response.setRspDesc("SUCCESS"); return response; } }
3、编写User和UserResponse类;
4、编写Axis2MessageReceiverInOut类,用来接收报文的:
package com.study.axis2.receive; import java.io.ByteArrayInputStream; import java.util.Iterator; 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.axiom.om.impl.builder.StAXOMBuilder; import org.apache.axiom.soap.SOAPBody; import org.apache.axiom.soap.SOAPEnvelope; import org.apache.axiom.soap.SOAPFactory; import org.apache.axis2.AxisFault; import org.apache.axis2.context.MessageContext; import org.apache.axis2.receivers.AbstractInOutMessageReceiver; public class Axis2MessageReceiverInOut extends AbstractInOutMessageReceiver{ private static String ENCODEING = "UTF-8"; private static String NAMING_SPACE = "http://impl.service.axis2.study.com"; @Override public void invokeBusinessLogic(MessageContext envMsg, MessageContext newEnvMsg) throws AxisFault { System.out.println("------------------------------------"); String body = envMsg.getEnvelope().getBody().toString(); System.out.println("request body1 : " + body); //Axis2Service axis2 = new Axis2ServiceImpl(); StringBuffer soapRequestData = new StringBuffer(); soapRequestData.append("<userResponse>"); soapRequestData.append("<rspCode>0000</rspCode>"); soapRequestData.append("<rspDesc>SUCCESS</rspDesc>"); soapRequestData.append("</userResponse>"); SOAPEnvelope env = toEnvelope(soapRequestData.toString()); newEnvMsg.setEnvelope(env); } public static SOAPEnvelope toEnvelope(String sourceXml) { String xmlBody = sourceXml; OMFactory of = OMAbstractFactory.getOMFactory(); OMNamespace bname = of.createOMNamespace(NAMING_SPACE, ""); SOAPFactory s12f = OMAbstractFactory.getSOAP12Factory(); SOAPEnvelope s12e = s12f.getDefaultEnvelope(); try { byte[] bytes = xmlBody.getBytes(ENCODEING); ByteArrayInputStream is = new ByteArrayInputStream(bytes); StAXOMBuilder builder = new StAXOMBuilder(is); OMElement elementBody = builder.getDocumentElement(); //elementBody.setNamespace(bname); //addNameSpacePrefix(elementBody, bname); SOAPBody s12b = s12e.getBody(); s12b.addChild(elementBody); } catch (Exception e) { e.printStackTrace(); } return s12e; } public static OMElement addNameSpacePrefix(OMElement element, OMNamespace prefix) { if (element.getChildElements() != null) { Iterator<OMElement> it = element.getChildElements(); element.setNamespace(prefix); while (it.hasNext()) { OMElement childelement = (OMElement) it.next(); childelement.setNamespace(prefix); if ((childelement.getChildElements() == null) || (!childelement.getChildElements().hasNext())) continue; addChildNameSpacePrefix(childelement.getChildElements(), prefix); } } return element; } public static void addChildNameSpacePrefix(Iterator element, OMNamespace prefix) { if (element != null) { while (element.hasNext()) { OMElement childelement = (OMElement) element.next(); childelement.setNamespace(prefix); if ((childelement.getChildElements() == null) || (!childelement.getChildElements().hasNext())) continue; addChildNameSpacePrefix(childelement.getChildElements(), prefix); } } } }
5、将该类和services.xml文件都放在aar下
时间: 2024-11-09 14:44:09