Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EclipseLink/Examples/MOXy/BVinJAXB/GettingStarted

Overview

This example demonstrates marshalling and unmarshalling of Java Objects to XML Documents using EclipseLink MOXy (JAXB).

Domain Model

Our examples will refer to the following domain model, represented by customer object.

package example.gettingstarted;
 
import java.util.ArrayList;
import java.util.List;
 
@XmlRootElement
public class Customer {
 
    @NotNull
    @Size(min = 3, max = 15)
    @XmlElement
    private String name;
 
    public Customer() {
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
}

Runtime - Marshalling 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(Customer.class);
        Marshaller marshaller = context.createMarshaller();
 
        Customer customer = new Customer();
        customer.setName("CafeBabe");
 
        marshaller.marshal(customer, new File("customer.xml"));
 
        // No additional configuration, running with Bean Validation in JAXB :-)
    }
}


Runtime - Marshalling object and handling BV exception

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(Customer.class);
        Marshaller marshaller = context.createMarshaller();
 
        Customer customer = new Customer();
        // customer.setName("CafeBabe");
 
        try {
            marshaller.marshal(customer, new File("customer.xml"));
        } catch (BeanValidationException bve) {
            System.out.println("Invalid customer detected:" + customer);
        }
 
    }
}



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 )


Changing BV mode

By default, BV is in mode AUTO. That means, if BV provider is found on classpath, it is turned on, otherwise it is silently turned off and an informative message is logged.

If exception should be thrown when no BV provider is found on classpath, set mode ON. A property must be passed to either JAXBContext, Marshaller or Unmarshaller.
To forcefully turn BV off, set mode OFF.

Example:

 Map<String, BeanValidationMode> props = new HashMap<>();
 props.put(JAXBContextProperties.BEAN_VALIDATION_MODE, BeanValidationMode.ON);
 Class[] classes = new Class[] { Customer.class };
 JAXBContext jaxbContext = JAXBContext.newInstance(classes, props);

Back to the top