我需要编写自定义的消息接收器类,而不是使用in-out中的RPCMessageReceiver
。我尝试过这样做,但它没有用。我是Web服务和axis2的新手。任何人都可以帮助我解决这个问题。
package com.webservice;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.stream.XMLStreamException;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.util.AXIOMUtil;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.description.AxisOperation;
import org.apache.axis2.receivers.AbstractInOutSyncMessageReceiver;
import org.apache.axis2.util.JavaUtils;
import request.Address;
import request.TestRequest;
import response.TestResponse;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;
@SuppressWarnings("deprecation")
public class MyServiceMessageReceiver extends AbstractInOutSyncMessageReceiver{
@Override
public void invokeBusinessLogic(MessageContext inMessageContext, MessageContext outMessageContext)
throws AxisFault {
Object obj = getTheImplementationObject(inMessageContext) ;
MyService webServiceImpl = (MyService) obj ;
AxisOperation operation = inMessageContext.getOperationContext().getAxisOperation();
if (operation == null && operation.getName() != null) {
throw new AxisFault("Operation is not located. SOAP ACTION must be specified") ;
}
String methodName = JavaUtils.xmlNameToJava(operation.getName().getLocalPart() ) ;
if (methodName == null) {
throw new AxisFault("Method with given name is not found") ;
}
if ("getEmployeeDetails".equalsIgnoreCase(methodName)) {
SOAPEnvelope soapEnvelope = inMessageContext.getEnvelope();
OMElement processTransactionElem = soapEnvelope.getBody().getFirstElement();
TestResponse testResponses = fromOM (webServiceImpl, processTransactionElem , getEnvelopeNamespaces(soapEnvelope));
if (testResponses != null) {
OMElement outelem = null ;
try {
outelem = convertToOM (testResponses);
} catch (XMLStreamException e) {
e.printStackTrace();
} inMessageContext.getEnvelope().getBody().setFirstChild(outelem) ;
outMessageContext.setEnvelope(inMessageContext.getEnvelope()) ;
} else {
System.out.println("Not Implemented");
}
}
private OMElement convertToOM(TestResponse testResponse) throws XMLStreamException {
XStream xstream = new XStream(new StaxDriver());
xstream.alias("testResponse",TestResponse.class);
String xmlStr = xstream.toXML(testResponse);
OMElement omele = AXIOMUtil.stringToOM(xmlStr);
return omele;
}
private TestResponse fromOM(MyService webServiceImpl, OMElement processTransactionElem, Map envelopeNamespaces) {
TestResponse testResponses = null;
try {
Iterator<OMElement> childElemIterator = processTransactionElem.getChildElements();
while (childElemIterator.hasNext()) {
OMElement childElem = childElemIterator.next();
String childName = childElem.getLocalName();
XStream stream = new XStream(new StaxDriver());
/*if("empId".equalsIgnoreCase(childName)){
empId = childElem.getText();
}*/
if ("getEmployeeDetails".equalsIgnoreCase(childName)) {
stream.alias("getEmployeeDetails", TestRequest.class);
stream.addImplicitArray(TestRequest.class, "address");
stream.alias("address", Address.class);
TestRequest request = (TestRequest)stream.fromXML(childName.toString());
testResponses = webServiceImpl.getEmployeeDetails(request);
}
return testResponses ;
}
} catch (Exception e) {
e.printStackTrace() ;
}
return null ;
}
private Map getEnvelopeNamespaces(SOAPEnvelope env){
Map returnMap = new HashMap();
OMNamespace ns;
for(Iterator namespaceIterator = env.getAllDeclaredNamespaces(); namespaceIterator.hasNext(); returnMap.put(ns.getPrefix(), ns.getNamespaceURI()))
ns = (OMNamespace)namespaceIterator.next();
return returnMap;
}
}
当我尝试发送请求并通过SOAP UI接收响应时,它将使用故障代码抛出响应,如soapenv:Receiver。这意味着服务器不处理该消息。