【问题标题】:Rest Template - XML IndentationRest 模板 - XML 缩进
【发布时间】:2017-08-12 01:51:19
【问题描述】:

在 Spring Boot 应用程序中使用 Rest Template 时,我需要使用新行和制表符缩进生成的 XML。如何在此 REST 模板中设置 JAXB Marshaller 的缩进属性。

Spring REST 模板代码:

    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 模板配置代码:

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

【问题讨论】:

    标签: spring rest spring-boot jaxb spring-integration


    【解决方案1】:

    我通过以下方式注入 Jaxbmarshaller,用下面的代码解决了这个问题:

    @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;
    }
    

    【讨论】:

      【解决方案2】:

      您必须向该RestTemplate bean 注入Jaxb2RootElementHttpMessageConverter 的扩展。并实现它的方法:

      /**
       * 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) {
      }
      

      为此提供财产:

      setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-30
        • 1970-01-01
        • 1970-01-01
        • 2012-03-07
        相关资源
        最近更新 更多