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();
}
You have to inject into that
RestTemplate
bean an extension for theJaxb2RootElementHttpMessageConverter
. And implement its method:providing property for that:
I fixed this issue with the below code by injecting the Jaxbmarshaller in the below way: