¿Cómo obtener una salida xml formateada de jaxb en primavera?


Estoy usando Jaxb2Marshaller como una propiedad view para ContentNegotiatingViewResolver. Soy capaz de obtener el xml repsonse. ¿Cómo lo formateo (impresión bonita)?

<bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="xml" value="application/xml" />
        </map>
    </property>
    <property name="defaultViews">
        <list>

            <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                <constructor-arg>
                    <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
                        <property name="classesToBeBound">
                            <list>

                            </list>
                        </property>
                    </bean>
                </constructor-arg>
            </bean>
        </list>
    </property>

</bean>
 27
Author: outvir, 2011-02-03

5 answers

<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>
 37
Author: Ritesh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-02-04 05:49:39

Intente establecer esta propiedad en su objeto marshaller:

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

Aquí está el Javadoc completo para la interfaz Marshaller. Echa un vistazo a la sección Resumen de campo.

 21
Author: jmort253,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-02-03 07:04:19

La respuesta de Ritesh no funcionó para mí. Tuve que hacer lo siguiente:

<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>
 8
Author: Doppelganger,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-03-06 22:12:20

Estaba buscando esto y pensé en compartir el código equivalente

@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;
}
 2
Author: DeezCashews,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-09 04:02:20

Use jaxb.formatear.salida en lugar de javax.XML.unir.Marshaller.JAXB_FORMATTED_OUTPUT as

Map<String,Object> map = new HashMap<String,Object>();
map.put("jaxb.formatted.output", true);
jaxb2Marshaller.setMarshallerProperties(map);
 0
Author: jnrprog,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-12 12:42:07