java 如何在XML prolog中设置standalone=“yes”

jv4diomz  于 2023-06-28  发布在  Java
关注(0)|答案(2)|浏览(495)

我已经创建了一个bean,以便在我的JacksonXML响应中包含XML序言。

@Bean
    public MappingJackson2XmlHttpMessageConverter mappingJackson2XmlHttpMessageConverter(Jackson2ObjectMapperBuilder builder) throws XMLStreamException {
        XmlMapper xmlMapper = builder.createXmlMapper(true).build();
        xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
        
        return new MappingJackson2XmlHttpMessageConverter(xmlMapper);
    }

但是,我需要在序言中包含standalone="yes"。我不知道如何添加此附加属性?我的Sping Boot 版本是3.0.6,Jackson版本是2.14.2

u0njafvf

u0njafvf1#

要使用Sping Boot 和Jackson在XML prolog中包含standalone=“yes”属性,您可以通过提供自定义Jackson2ObjectMapperBuilder示例来自定义XmlMapper配置。下面是包含standalone属性的代码的更新版本:

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.http.converter.xml.Jackson2ObjectMapperBuilder;

@Configuration
public class JacksonXmlConfig {

    @Bean
    public MappingJackson2XmlHttpMessageConverter mappingJackson2XmlHttpMessageConverter(Jackson2ObjectMapperBuilder builder) {
        XmlMapper xmlMapper = builder.createXmlMapper(true).build();
        xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
        xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_1_0_BOM, false);
        xmlMapper.configure(ToXmlGenerator.Feature.STANDALONE, "yes"); // Set the standalone attribute
        
        return new MappingJackson2XmlHttpMessageConverter(xmlMapper);
    }

}

在这个更新的代码中,xmlMapper.configure(ToXmlGenerator.Feature.STANDALONE,“yes”)行将XML序言中的standalone属性设置为“yes”。这将在生成的XML响应中包含standalone=“yes”属性。
请注意,ToXmlGenerator.Feature.STANDALONE特性从Jackson版本2.13.0开始可用。既然你提到你的Jackson版本是2.14.2,它应该可以正常工作。
请确保在Sping Boot 应用程序中或适当的组件扫描位置包含此配置类,以使其生效。

krugob8w

krugob8w2#

要在XML序言中包含standalone=“yes”属性,可以使用Jackson的XmlMapper实现自定义XML编写器,并覆盖负责编写XML序言的方法。下面是一个例子:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;
import java.io.Writer;

public class CustomXmlMapper extends XmlMapper {

    @Override
    protected ToXmlGenerator _createGenerator(Writer out, JsonGenerator originalGenerator) throws IOException {
        ToXmlGenerator xmlGenerator = super._createGenerator(out, originalGenerator);
        xmlGenerator.enable(ToXmlGenerator.Feature.WRITE_XML_DECLARATION);
        xmlGenerator.configure(ToXmlGenerator.Feature.WRITE_XML_1_1, true);
        xmlGenerator.configure(ToXmlGenerator.Feature.WRITE_XML_1_0_BOM, false);

        // Write the XML prolog with the standalone attribute
        xmlGenerator.writeRaw("<?xml version=\"1.0\" encoding=\"" + xmlGenerator.getOutputContext().getEncoding().getJavaName() + "\" standalone=\"yes\"?>\n");

        return xmlGenerator;
    }

    // Additional customization methods can be added here if needed

}

在此示例中,通过扩展XmlMapper创建了一个名为CustomXmlMapper的自定义XmlMapper类。_createGenerator()方法被重写以自定义XML生成过程。在该方法中,XML序言是用standalone=“yes”属性手动编写的。
要在Sping Boot 应用程序中使用此自定义XmlMapper,请按如下方式修改配置代码:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.http.converter.xml.Jackson2ObjectMapperBuilder;

@Configuration
public class JacksonXmlConfig {

    @Bean
    public MappingJackson2XmlHttpMessageConverter mappingJackson2XmlHttpMessageConverter(Jackson2ObjectMapperBuilder builder) {
        XmlMapper xmlMapper = new CustomXmlMapper(); // Use the custom XmlMapper
        xmlMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);

        return new MappingJackson2XmlHttpMessageConverter(xmlMapper);
    }

}

在此更新的代码中,我们使用CustomXmlMapper而不是默认的XmlMapper,以确保使用自定义XML编写器。
请注意,此解决方案要求您使用XmlMapper的自定义实现并覆盖XML生成过程。

相关问题