30

I am using Jaxb2Marshaller as a view property for ContentNegotiatingViewResolver. I am able to get the xml repsonse. How do I format (pretty print) it?

<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>
outvir
  • 511
  • 4
  • 8
  • 11

6 Answers6

40
<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>
jmort253
  • 33,123
  • 11
  • 95
  • 117
Ritesh
  • 7,322
  • 2
  • 38
  • 42
24

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.

jmort253
  • 33,123
  • 11
  • 95
  • 117
11

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;
}
Andrew Wynham
  • 2,152
  • 20
  • 24
8

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>
Doppelganger
  • 125
  • 1
  • 7
0

Another way to achieve the purpose use stringwriter class.

public class geoClientSpringWS extends WebServiceGatewaySupport
{

    public GetGeoIPResponse getGeoIPResponse(String inputval) throws JAXBException
    {
        String endpointuri="http://Kondle-PC:8088/mockGeoIPServiceSoap";
        GetGeoIP requestobj= new GetGeoIP();
        requestobj.setIPAddress(inputval);

        Jaxb2Marshaller marshaller= new Jaxb2Marshaller();

        StringWriter textwriter= new StringWriter();    
        JAXBContext jaxbContext = JAXBContext.newInstance(GetGeoIP.class);
        jaxbContext.createMarshaller().marshal(requestobj, textwriter);

        String textwriteroutput=textwriter.toString();

        System.out.println("response : " + textwriteroutput);   

        GetGeoIPResponse responseobj=(GetGeoIPResponse) getWebServiceTemplate().marshalSendAndReceive(endpointuri, requestobj);

        return responseobj;
    }
}
MarGin
  • 1,780
  • 1
  • 15
  • 25
sudhirkondle
  • 117
  • 1
  • 5
  • If you are concerned about the performance then perhaps a byte array stream writer will be a better choice. Also see https://stackoverflow.com/questions/24955072. The `jaxbContext` can also be moved out to private static. Also `marshaller` is not used and can be removed. – Ritesh Dec 28 '19 at 15:13
-1

Use jaxb.formatted.output instead of 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);
  • 1
    Why? Value of `javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT` is `jaxb.formatted.output` (see [here](https://docs.oracle.com/javase/7/docs/api/constant-values.html#javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT)). – Piro Sep 19 '19 at 08:05