2011-02-03 16 views
27

Sto utilizzando Jaxb2Marshaller come proprietà di visualizzazione per ContentNegotiatingViewResolver. Sono in grado di ottenere il replay xml. Come si formatta (piuttosto stampato)?Come ottenere l'output xml formattato da jaxb in primavera?

<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> 

risposta

36
<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> 
+0

Grazie, ha fatto il trucco. – outvir

21

provare a impostare questa proprietà sul tuo oggetto marshaller:

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

Ecco la Javadoc completo per l'interfaccia Marshaller. Controlla la sezione Riepilogo dei campi.

8

La risposta di Ritesh non ha funzionato per me. Ho dovuto fare quanto segue:

<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> 
2

cercavo per questo e ho pensato di condividere il codice 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; 
} 
0

Uso jaxb.formatted.output invece di javax.xml.bind.Marshaller. JAXB_FORMATTED_OUTPUT as

Map<String,Object> map = new HashMap<String,Object>(); 
map.put("jaxb.formatted.output", true); 
jaxb2Marshaller.setMarshallerProperties(map); 
Problemi correlati