简体   繁体   中英

Generating SOAP XML from POJO

Hello for loging and debuging purpose I need to store input POJO of spring bean in DB in SOAP XML form. Can you help me which library i can use and do you have some code examples how to create SOAP XML from POJO object. I try to use javax.xml.soap.* to generate SOAP Envelope, Header and Body, and JAXB for generating xml from POJO. javax.xml.soap.* works fine, but i have problem with namespaces in POJO xml. Is there way to generate namespaces automaticaly? For example...

My output without namespaces is

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header/>
  <SOAP-ENV:Body>
    <z:getClientDetail xmlns:z="my.package">
    <client>
      <adresses>
        <city>Praha</city>
        <houseNumber>1455</houseNumber>
        <street>Hudeckova</street>
      </adresses>
      <adresses>
        <city>Brno</city>
        <houseNumber>44</houseNumber>
        <street>Tupolevova</street>
      </adresses>
      <firstName>Standa</firstName>
      <lastName>Vrana</lastName>
    </client>
 </z:getClientDetail>

But correct output with namespace is

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header/>
  <SOAP-ENV:Body>
    <z:getClientDetail xmlns:z="my.package">
    <java:client xmlns:java="my.package.dto">
    <java:adresses>
      <java:city>Praha</city>
      <java:houseNumber>1455</houseNumber>
      <java:street>Hudeckova</street>
    </java:adresses>
    <java:adresses>
      <java:city>Brno</city>
      <java:houseNumber>44</houseNumber>
      <java:street>Tupolevova</street>
    </java:adresses>
    <java:firstName>Standa</firstName>
    <java:lastName>Vrana</lastName>
   </java:client>
  </z:getClientDetail>

Thanks P.

Why do you want to store data in DB in SOAP XML form?

And to create SOAP XML from POJO object, you can write manually all the static part and put all dynamic values from POJO like below. Ex:

String request = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">  <soapenv:Header><tem:B2BCode>"+pojo.getPojoObject+"</tem:B2BCode></soapenv:Header></soapenv:Envelope>"

You can use the package level annotation javax.xml.bind.annotation.XmlSchema to accomplish this. JAXB will then generate the XML from the pojo's in the annotated package in the given namespace.

If you would prefer you can also do this on a class-by-class basis using the javax.xml.bind.annotation.XmlType annotation or on a field-by-field basis using the javax.xml.bind.annotation.XmlElement annotation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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