use of org.jboss.resteasy.plugins.providers.jaxb.XmlNamespacePrefixMapper in project resteasy by resteasy.
the class JaxbNamespacePrefixTest method testNamespacePrefix.
/**
* @tpTestDetails Create xml schema from provided class and set "namespacePrefixMapper" for the Marshaller
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testNamespacePrefix() throws Exception {
JAXBContext ctx = JAXBContext.newInstance(JaxbNameSpacePrefixPurchaseOrderType.class);
JaxbNameSpacePrefixPurchaseOrderType po = new JaxbNameSpacePrefixPurchaseOrderType();
JaxbNameSpacePrefixItems jaxbNameSpacePrefixItems = new JaxbNameSpacePrefixItems();
JaxbNameSpacePrefixItem jaxbNameSpacePrefixItem = new JaxbNameSpacePrefixItem();
jaxbNameSpacePrefixItem.setComment("Tetsing");
jaxbNameSpacePrefixItem.setPartNum("242-GZ");
jaxbNameSpacePrefixItem.setProductName("My Thing");
jaxbNameSpacePrefixItem.setQuantity(6);
jaxbNameSpacePrefixItem.setUSPrice(new BigDecimal(13.99));
jaxbNameSpacePrefixItems.getJaxbNameSpacePrefixItem().add(jaxbNameSpacePrefixItem);
po.setJaxbNameSpacePrefixItems(jaxbNameSpacePrefixItems);
Marshaller marshaller = RiHacks.createMarshaller(ctx);
XmlSchema xmlSchema = JaxbNameSpacePrefixPurchaseOrderType.class.getPackage().getAnnotation(XmlSchema.class);
Assert.assertNotNull("Couldn't create xml schema for JaxbNameSpacePrefixPurchaseOrderType class", xmlSchema);
XmlNamespacePrefixMapper mapper = new XmlNamespacePrefixMapper(xmlSchema.xmlns());
try {
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", mapper);
} catch (PropertyException e) {
logger.error(e.getMessage(), e);
}
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
ObjectFactory factory = new ObjectFactory();
ByteArrayOutputStream out = new ByteArrayOutputStream();
marshaller.marshal(factory.createPurchaseOrder(po), out);
}
Aggregations