休息模板-XML缩进 [英] Rest Template - XML Indentation
本文介绍了休息模板-XML缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Spring Boot应用程序中使用Rest Template时,我需要使用新的行和制表符来缩进生成的XML.如何在此REST模板中设置JAXB Marshaller的缩进属性.
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模板代码:
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);
其他模板配置代码:
@Bean
@Qualifier("restTemp")
public RestTemplate restTemplate(RestTemplateBuilder builder,
CloseableHttpClient httpClient) {
return builder.requestFactory(new HttpComponentsClientHttpRequestFactory(httpClient)).build();
}
推荐答案
我通过以下方式注入Jaxbmarshaller,从而通过以下代码解决了此问题:
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;
}
这篇关于休息模板-XML缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文