1

我正在使用 SpringJaxb2Marshaller将 java 对象转换为 XML,反之亦然。考虑到示例,我需要为xmlns prefix和- 设置动态值value

xmlns:abc="http://www.example.com"

其中prefixasabcvalueashttp://www.example.com必须是可配置的(从属性文件提供)。

Product查看包下类的示例xmlcom.test.abc

<abc:Product 
    xmlns:abc="http://www.example.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <abc:productId>252</abc:productId>
    <abc:productName>XYZ</abc:productName>
</abc:Product>

要构建此 xml,我使用以下配置

Spring Jaxb2Marshaller Bean 配置

@Bean
public Jaxb2Marshaller getJaxb2Marshaller(){
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setPackagesToScan("com.test.abc", "com.test.xyz");
    Map<String,Object> propertiesMap = new HashMap<String,Object>();
    propertiesMap.put("jaxb.formatted.output", true);
    marshaller.setMarshallerProperties(propertiesMap);
    return marshaller;
}

包信息.java

@javax.xml.bind.annotation.XmlSchema(
    namespace = "http://www.example.com", 
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
    xmlns = { @javax.xml.bind.annotation.XmlNs(namespaceURI = "http://www.example.com", prefix="abc")})
package com.test.abc;

在这里,我是硬编码的 xmlns 前缀和值。我需要从属性文件中提供 xmlns 前缀和值。我怎样才能做到这一点?

我正在使用 SpringBoot 1.3.3

4

0 回答 0