【问题标题】:Convert SOAP Response to JAVA Object将 SOAP 响应转换为 JAVA 对象
【发布时间】:2018-05-21 11:14:33
【问题描述】:

我需要将一个肥皂响应 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。 我正在调用使用spring框架的webService模板的soapService。 代码是

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

当我尝试打印我得到的soapResponse 时,问题一开始就发布了 xml。 有什么方法可以实现吗?

【问题讨论】:

    标签: java json soap pojo


    【解决方案1】:

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

    在 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 类。如果没有,请这样做。 你可以参考下面一二生成java类。 JAXB - Mapping SOAP to Java Classes

    【讨论】:

    • soapString 的类型是什么。就我而言,它是 SOAPMessage。
    • 是的,使用 weservice 模板。
    【解决方案2】:
    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 文件

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多