Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
EclipseLink/Examples/MOXy/BVinJAXB/GettingStarted
< EclipseLink | Examples | MOXy | BVinJAXB
Contents
Overview
No metadata is required to convert your existing object model to XML. This example will demonstrate how easy it is to convert objects to XML using EclipseLink MOXy (JAXB). In later examples we will supply metadata to customize the XML representation.
Domain Model
For this example our domain model will be represented employee object.
package example.gettingstarted; import java.util.ArrayList; import java.util.List; @XmlRootElement public class Employee { @NotNull @Size(min = 3, max = 15) @XmlElement private String name; public Employee() { } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Runtime - Marshalling valid object
package example.gettingstarted; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import java.io.File; public class Main { public static void main(String[] args) throws Exception { JAXBContext context = JAXBContext.newInstance(Employee.class); Marshaller marshaller = context.createMarshaller(); Employee employee = new Employee(); employee.setName("CafeBabe"); marshaller.marshal(employee, new File("emp.xml")); // No additional configuration, running with Bean Validation in JAXB :-) } }
Runtime - Marshalling invalid object
package example.gettingstarted; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import java.io.File; public class Main { public static void main(String[] args) throws Exception { JAXBContext context = JAXBContext.newInstance(Employee.class); Marshaller marshaller = context.createMarshaller(); Employee employee = new Employee(); // employee.setName("CafeBabe"); try { marshaller.marshal(employee, new File("emp.xml")); } catch (BeanValidationException bve) { System.out.println("Invalid employee detected:" + employee); } // No additional configuration, running with Bean Validation in JAXB :-) } }
Generating schema with constraints
package example.gettingstarted; import javax.xml.bind.JAXBContext; import javax.xml.bind.SchemaOutputResolver; import javax.xml.transform.Result; import javax.xml.transform.stream.StreamResult; import java.io.File; import java.io.IOException; public class Main { public static void main(String[] args) throws Exception { JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class); SchemaOutputResolver sor = new MySchemaOutputResolver(); jaxbContext.generateSchema(sor); } static class MySchemaOutputResolver extends SchemaOutputResolver { public Result createOutput(String uri, String suggestedFileName) throws IOException { File file = new File(suggestedFileName); StreamResult result = new StreamResult(file); result.setSystemId(file.toURI().toURL().toString()); return result; } } }
XJC: Bean Validation Plugin
Command Line
xjc file.xsd -XBeanVal
Example usage with mods:
xjc file.xsd -XBeanVal jsr303 simpleRegex
Programmatically
Driver.run ( new String [ ] { schemaPath, “-extension”, “-XBeanVal” }, System.out, System.out )
Turn BV in JAXB off
Map<String, BeanValidationMode> props = new HashMap<>(); props.put(JAXBContextProperties.BEAN_VALIDATION_MODE, BeanValidationMode.NONE); Class[] classes = new Class[] { Customer.class }; JAXBContext jaxbContext = JAXBContext.newInstance(classes, props);