1

I am using Spring Jaxb2Marshaller to convert java objects to XML and vice versa. I need to set dynamic values for xmlns prefix and value - means, considering the example

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

where prefix as abc and value as http://www.example.com must be configurable(supply from a properties file).

See the sample xml of class Product under the package com.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>

To build this xml, I am using following configurations

Spring Jaxb2Marshaller Bean Configuration

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

pack-info.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;

Here I am hardcoded xmlns prefix and value. I need to supply the xmlns prefix and value from a properties file. How I can achieve this ?

I am using SpringBoot 1.3.3

faizi
  • 1,265
  • 2
  • 12
  • 28

0 Answers0