0

I'm trying to return an xml response with JAXB and the implementation looks like in the example bellow. My question is: There is a way to return the whole xml without setting a value on the field?

    @ResponsePayload
    public JAXBElement<Ns2AnfrageBonitaetsauskunftAntwortType> getResponse(@RequestPayload JAXBElement<Ns2AnfrageBonitaetsauskunftTyp> request) {

        Ns2AnfrageBonitaetsauskunftAntwortType response = new Ns2AnfrageBonitaetsauskunftAntwortType();
        response.setSchufaReferenz("test2");
        response.setTeilnehmerreferenz("test1");
        response.setAktionsdaten("test3");
        Ns3BonitaetsauskunftType bonita = new Ns3BonitaetsauskunftType();
        bonita.setTeilnehmerkennung("test4");

        Ns3VerarbeitungsinformationType verar = new Ns3VerarbeitungsinformationType();
        verar.setErgebnistyp("test7");
        bonita.setVerarbeitungsinformation(verar);

        Ns3VerbraucherdatenAuskunftType daten = new Ns3VerbraucherdatenAuskunftType();
        daten.setPersonOhneGeburtsdatum("test6");
        bonita.setVerbraucherdaten(daten);
        response.setReaktion(bonita);
        ObjectFactory objectFactory = new ObjectFactory();
        return objectFactory.createBonitaetsauskunft(response);
    }

At the moment the response look like this:

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
   <env:Header/>
   <env:Body>
      <ns3:Bonitaetsauskunft xmlns:ns3="http://ifd-schema.de/BonitaetsauskunftSCHUFA">
         <SchufaReferenz>test2</SchufaReferenz>
         <Teilnehmerreferenz>test1</Teilnehmerreferenz>
         <Aktionsdaten xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">test3</Aktionsdaten>
         <Reaktion>
            <Teilnehmerkennung>test4</Teilnehmerkennung>
            <Verbraucherdaten>
               <PersonOhneGeburtsdatum>test6</PersonOhneGeburtsdatum>
            </Verbraucherdaten>
            <Verarbeitungsinformation>
               <Ergebnistyp>test7</Ergebnistyp>
            </Verarbeitungsinformation>
         </Reaktion>
      </ns3:Bonitaetsauskunft>
   </env:Body>
</env:Envelope>

I have a lot of more field on which i don't want to set a value but i want them to be in the response. Any ideas?

Thank you

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
Alexandra
  • 21
  • 3
  • Having XmlAddapter for each relevant type. The question is, for example for String, how do you then distinguish between an empty String and a null-String? Anyway look at https://stackoverflow.com/questions/18921196/jaxb-empty-element-unmarshalling – Michal Mar 22 '19 at 11:12

1 Answers1

0

You can add to each property nillable attribut using @XmlElement(nillable = true) annotation. From documentation:

Optional. Specifies whether an explicit null value can be assigned to the element. True enables an instance of the element to have the null attribute set to true. The null attribute is defined as part of the XML Schema namespace for instances. Default is false

Fields with this attribute are print as <value xsi:nil="true"/>. Small example:

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

public class JaxbApp {

    public static void main(String[] args) throws Exception {
        Root dataFile = new Root();
        dataFile.setPerson(new Person());

        JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);

        marshaller.marshal(dataFile, System.out);
    }
}

@XmlRootElement(name = "roo")
@XmlAccessorType(XmlAccessType.FIELD)
class Root {

    @XmlElement(nillable = true)
    private Integer id;

    @XmlElement(nillable = true)
    private String value;

    @XmlElement(nillable = true)
    private Person person;

    // getters, setters, toString
}

@XmlAccessorType(XmlAccessType.FIELD)
class Person {

    @XmlElement(nillable = true)
    private String name;

    @XmlElement(nillable = true)
    private String lastName;

    // getters, setters, toString
}

prints:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<roo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
    <id xsi:nil="true"/>
    <value xsi:nil="true"/>
    <person>
        <name xsi:nil="true"/>
        <lastName xsi:nil="true"/>
    </person>
</roo>

In that case you need to change your XSD/WSDL document and add this attribute to each primitive field. Of course, you still need to create new POJO instance like new Person() in above example. You can of course set each property in class to some kind of default value private String value = ""; or write reflection tool which travers all properties and set to some custom values.

See also:

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146