Rest Template - XML Indentation

2019-09-17 17:03发布

问题:

I need my generated XML to be indented with new lines and tabs when using Rest Template in Spring Boot application. How can I set the indentation property of JAXB Marshaller in this REST Template.

Spring REST template code:

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_XML);
    headers.add("Authorization", "Basic " +  Base64Utility.encode(userAndPass.getBytes()));

    Xml documentDefinition = myfactory.createObjects(StudentBean, ClassBean, CollegeBean);

   HttpEntity<Xml> request = new HttpEntity<>(documentDefinition, headers);

   URI result = restTemplate.postForLocation(builder.toUriString(), request);

Rest Template Configuration Code:

@Bean
@Qualifier("restTemp")
public RestTemplate restTemplate(RestTemplateBuilder builder,
                                 CloseableHttpClient httpClient) {
    return builder.requestFactory(new HttpComponentsClientHttpRequestFactory(httpClient)).build();
}

回答1:

You have to inject into that RestTemplate bean an extension for the Jaxb2RootElementHttpMessageConverter. And implement its method:

/**
 * Customize the {@link Marshaller} created by this
 * message converter before using it to write the object to the output.
 * @param marshaller the marshaller to customize
 * @since 4.0.3
 * @see #createMarshaller(Class)
 */
protected void customizeMarshaller(Marshaller marshaller) {
}

providing property for that:

setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);


回答2:

I fixed this issue with the below code by injecting the Jaxbmarshaller in the below way:

@Bean
@Qualifier("restTemplate")
public RestTemplate restTemplate(RestTemplateBuilder builder,
                                 CloseableHttpClient httpClient) {
    RestTemplate restTemplate = builder.requestFactory(new HttpComponentsClientHttpRequestFactory(httpClient)).build();

    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(getMarshallingHttpMessageConverter());
    converters.add(new Jaxb2RootElementHttpMessageConverter());
    converters.add(new MappingJackson2HttpMessageConverter());
    restTemplate.setMessageConverters(converters);
    return restTemplate;
}

@Bean(name = "marshallingHttpMessageConverter")
public MarshallingHttpMessageConverter getMarshallingHttpMessageConverter() {
    MarshallingHttpMessageConverter marshallingHttpMessageConverter = new MarshallingHttpMessageConverter();
    marshallingHttpMessageConverter.setMarshaller(getJaxb2Marshaller());
    marshallingHttpMessageConverter.setUnmarshaller(getJaxb2Marshaller());
    return marshallingHttpMessageConverter;
}

@Bean(name = "jaxb2Marshaller")
public Jaxb2Marshaller getJaxb2Marshaller() {
    Map<String, Object> props = new HashMap<>();
    props.put(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
    jaxb2Marshaller.setClassesToBeBound(Xml.class);
    jaxb2Marshaller.setMarshallerProperties(props);
    return jaxb2Marshaller;
}