Search in sources :

Example 1 with JaxbNameSpacePrefixItems

use of org.jboss.resteasy.test.providers.resource.jaxbNameSpacePrefix.JaxbNameSpacePrefixItems 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);
}
Also used : Marshaller(jakarta.xml.bind.Marshaller) XmlNamespacePrefixMapper(org.jboss.resteasy.plugins.providers.jaxb.XmlNamespacePrefixMapper) ObjectFactory(org.jboss.resteasy.test.providers.resource.jaxbNameSpacePrefix.ObjectFactory) XmlSchema(jakarta.xml.bind.annotation.XmlSchema) JaxbNameSpacePrefixPurchaseOrderType(org.jboss.resteasy.test.providers.resource.jaxbNameSpacePrefix.JaxbNameSpacePrefixPurchaseOrderType) PropertyException(jakarta.xml.bind.PropertyException) JaxbNameSpacePrefixItems(org.jboss.resteasy.test.providers.resource.jaxbNameSpacePrefix.JaxbNameSpacePrefixItems) JAXBContext(jakarta.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JaxbNameSpacePrefixItem(org.jboss.resteasy.test.providers.resource.jaxbNameSpacePrefix.JaxbNameSpacePrefixItem) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

JAXBContext (jakarta.xml.bind.JAXBContext)1 Marshaller (jakarta.xml.bind.Marshaller)1 PropertyException (jakarta.xml.bind.PropertyException)1 XmlSchema (jakarta.xml.bind.annotation.XmlSchema)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 BigDecimal (java.math.BigDecimal)1 XmlNamespacePrefixMapper (org.jboss.resteasy.plugins.providers.jaxb.XmlNamespacePrefixMapper)1 JaxbNameSpacePrefixItem (org.jboss.resteasy.test.providers.resource.jaxbNameSpacePrefix.JaxbNameSpacePrefixItem)1 JaxbNameSpacePrefixItems (org.jboss.resteasy.test.providers.resource.jaxbNameSpacePrefix.JaxbNameSpacePrefixItems)1 JaxbNameSpacePrefixPurchaseOrderType (org.jboss.resteasy.test.providers.resource.jaxbNameSpacePrefix.JaxbNameSpacePrefixPurchaseOrderType)1 ObjectFactory (org.jboss.resteasy.test.providers.resource.jaxbNameSpacePrefix.ObjectFactory)1 Test (org.junit.Test)1