代码之家  ›  专栏  ›  技术社区  ›  Sean Nguyen

使用Axis1.2解析SOAPXML

  •  1
  • Sean Nguyen  · 技术社区  · 15 年前

    我正在为我的soap客户机使用axis 1.2。我需要编写一个单元测试,接受soap消息响应并将其解析为axis生成的java对象。我想不出办法。有人能帮我吗?

    谢谢,

    1 回复  |  直到 15 年前
        1
  •  0
  •   Sean Nguyen    9 年前

    我终于找到了办法。您可以通过创建一个模拟soap引擎来实现这一点,该引擎返回您想要的xml。然后axis将完成所有的解析和所有的逻辑,就像它是从http源获得的一样。下面是一个例子:

    public class SimulatorHandler extends BasicHandler
    {
    
    
        /**
         * System property to that hold soap response message in xml.
         */
        private static final String responseMsg = "soapResponse";
    
        /**
         * @return the responseMsg
         */
        public static String getResponseMsgSystemProperty()
        {
            return responseMsg;
        }
    
        public void invoke(MessageContext context) throws AxisFault
        {
    
                    // i haven't figure out a way to do a setter on this msg
                    // so I have to get it from system properties
            String msg = System.getProperty(responseMsg);
            ByteArrayInputStream is = new ByteArrayInputStream(msg.getBytes());
            Message response = new Message(is);
            response.setMessageType(Message.RESPONSE);
            context.setResponseMessage(response);
        }
    }
    
    推荐文章