从Java Web服务客户端获取原始XML响应

问题描述:

我正在尝试从Web服务获取原始XML响应,而不是通常的POJO集。

I am trying to get the raw XML response from a web service, instead of the usual set of POJOs.

我正在使用我生成的Web服务客户端(所以我可以从WSDL和一些模式访问客户端的代码。客户端是在RAD 7.5中生成的,我认为使用JAX-WS。我一直在查看客户端代码本身,但我甚至不确定客户端代码是否曾处理原始XML或是否将其传递给其他库。

I am using a webservice client that I generated (so I have access to the client's code) from a WSDL and some schemas. The client is generated in RAD 7.5, I think using JAX-WS. I've been looking at the client code itself, but I'm not even sure if the client code ever handles raw XML or if it passes it off to other libraries.

您可以使用

javax.xml.ws.handler.soap.SOAPHandler<javax.xml.ws.handler.soap.SOAPMessageContext>

你可以使用 SOAPMessageContext#getMessage()$ c来获取消息$ c>并使用方法将消息转换为String

you can simply get message using SOAPMessageContext#getMessage() and convert message to String using method

   public static String getXmlMessage(SOAPMessage message) throws Exception
   {
         ByteArrayOutputStream os = new ByteArrayOutputStream();
         message.writeTo(os);
         final String encoding = (String) message.getProperty(SOAPMessage.CHARACTER_SET_ENCODING);
         if (encoding == null)
         {
             return new String(os.toByteArray());
         }
         else
         {
            return new String(os.toByteArray(), encoding);    
         }
   }  

此外,您可以在此处阅读有关客户端SOAP处理程序的信息

文章

Also you can read here about SOAP handler on client side
Article