繁体   English   中英

将SOAP响应转换为JAVA对象

[英]Convert SOAP Response to JAVA Object

我需要将soap响应xml转换为JAVA对象,以将其提供给其他服务。 SOAP响应看起来像

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:myResponse xmlns:ns2="http://impl.service.abc.com/">
         <return>
            <response>
               <timestamp>11068446</timestamp>
               <txnAmount>1</txnAmount>
               <userGuid>11068446</userGuid>
               <walletSystemTxnId>123456789</walletSystemTxnId>
            </response>
            <status>SUCCESSS</status>
            <statusCode>SUCCESS</statusCode>
            <statusMessage>SUCCESS</statusMessage>
         </return>
      </ns2:myResponse>
   </S:Body>
</S:Envelope>

我尝试了很多东西来进行转换,以读取它并将其转换为我的类实例。

我如何称呼soapservice 我正在调用soapService,它正在使用spring框架的webService模板。 代码是

    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
    SOAPConnection soapConnection = soapConnectionFactory.createConnection();
    //Send SOAP Message to SOAP Server
   SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapWithdrawAction), soapEndpointUrl);

当我尝试打印soapResponse时,出现在问题开头的xml。 有什么办法可以做到这一点?

你能像下面这样尝试吗! 希望对你有帮助

在wsimport的帮助下使用所需的XSD创建MyResponse类

@Autowired
@Qualifier("repositoryWsTemplate")
private WebServiceTemplate repositoryWsTemplate;
 public Object executeSoapReqAndRes(Object object,WebServiceMessageCallback requestCallback){
MyResponse myResponseInstance =(MyResponse )repositoryWsTemplate
            .marshalSendAndReceive(object, requestCallback);
return myResponseInstance ;
}

注意:希望您使用wsdl来生成MyResponse类,否则请这样做。

int PRETTY_PRINT_INDENT_FACTOR = 4;
String TEST_XML_STRING =
            "<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>";

try {
     JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
     String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
     System.out.println(jsonPrettyPrintString);
} catch (JSONException je) {
     System.out.println(je.toString());
}

希望对您有所帮助,请使用org.json jar文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM