将Java Object转换为soap XML

时间:2017-12-01 10:37:54

标签: java rest web-services soap

说我有员工的对象

Employee employee = new Employee();
employeee.setId(1);
employee.setName("stack");

我希望将其转换为格式低于

的格式

这是我想要的xml fomat

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http:/someurl" xmlns:bas="http://someurl" xmlns:req="http://someurl" xmlns:sch="http://someurl">
   <soapenv:Body>

   <ns1:employeeReturn xmlns:ns1="someurl">
         <ns2:result xmlns:ns2="someurl">
            <ns3:resultCode xmlns:ns3="someurl">00000000</ns3:resultCode>
            <ns4:resultMessage xmlns:ns4="someurl">Success</ns4:resultMessage>
         </ns2:result>
         <ns5:resultList xmlns:ns5="http://someurl">
            <ns1:item>
               <ns6:resultCode xmlns:ns6="http://someurl">00000000</ns6:resultCode>
            </ns1:item>
         </ns5:resultList>
         <ns7:subResponseInfos xmlns:ns7="http://someurl">
            <ns1:item>
               <ns8:effectiveEndTime   xmlns:ns8="http://someurl">20361231170000</ns8:effectiveEndTime>
              </ns1:item>
              </ns1:employeeReturn >
                 </soapenv:Body>
</soapenv:Envelope>

我尝试按如下方式创建肥皂信息

SOAPMessage soapMessage = messageFactory.createMessage();

        SOAPPart soapPart = soapMessage.getSOAPPart();
        SOAPEnvelope envelope = soapPart.getEnvelope();
        SOAPBody body = envelope.getBody();

        envelope.addNamespaceDeclaration("xsi", "http://url");

        SOAPBodyElement element0 = body.addBodyElement(
                envelope.createName("employeereturn", "ns1", "http://url"));


        SOAPElement element1 = element0.addChildElement(
                body.addBodyElement(envelope.createName("result", "ns2", "http://url")));

        SOAPElement element2 = element1.addChildElement(body.addBodyElement(envelope.createName("resultCode", "ns3", "http://url"))).addTextNode("00000");

        element1.addChildElement(body.addBodyElement(envelope.createName("resultMessage", "ns4", "http://url"))).addTextNode("success");


        SOAPElement element3 = element0.addChildElement(
                    body.addBodyElement(envelope.createName("resultList", "ns5", "http://url")));

但随着您的员工对象变大并且有一些嵌套对象,将很难维护代码

0 个答案:

没有答案