How to get formatted xml output from jaxb in spring?

<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list> .... </list>
    </property>
    <property name="marshallerProperties">
        <map>
            <entry>
                <key>
                    <util:constant static-field="javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT" />
               </key>
              <value type="java.lang.Boolean">true</value>
            </entry>
        </map>
    </property>
</bean>

Ritesh's answer didn't work for me. I had to do the following:

<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list> ... </list>
    </property>
    <property name="marshallerProperties">
        <map>
            <entry key="jaxb.formatted.output">
                <value type="boolean">true</value>
            </entry>
        </map>
    </property>
</bean>

Try setting this property on your marshaller object:

 marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE )

Here is the full Javadoc for the Marshaller interface. Check out the Field Summary section.


Was looking for this and thought I'd share the code equivalent

@Bean
public Marshaller jaxbMarshaller() {
    Map<String, Object> props = new HashMap<String, Object>();
    props.put(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    Jaxb2Marshaller m = new Jaxb2Marshaller();
    m.setMarshallerProperties(props);
    m.setPackagesToScan("com.example.xml");
    return m;
}

Tags:

Spring

Jaxb