I was just looking to below questions:
How to get validation events with JaXB?
Validate nested object from complex object using jaxb
I had a similar problem so I tried these solutions but I found something odd:
Customer Validation
package com.oner.jaxb.test;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;
public class CustomerValidator implements ValidationEventHandler {
@Override
public boolean handleEvent(ValidationEvent event) {
System.out.println(event.getMessage());
System.out.println(event.getLocator().getObject());
return true;
}
}
Customers
package com.oner.jaxb.test.entities;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Customers {
private List<Customer> customers =
new ArrayList<Customer>();
@XmlElement(name="customer")
public List<Customer> getCustomers() {
return customers;
}
@Override
public String toString() {
return "Customers [customers=" + customers + "]";
}
}
Customer
package com.oner.jaxb.test.entities;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
public class Customer {
private String name;
private List<PhoneNumber> phoneNumbers =
new ArrayList<PhoneNumber>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement(name="phone-number")
public List<PhoneNumber> getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}
@Override
public String toString() {
return "Customer [name=" + name + ", phoneNumbers=" + phoneNumbers + "]";
}
}
PhoneNumber
package com.oner.jaxb.test.entities;
public class PhoneNumber {
@Override
public String toString() {
return "PhoneNumber []";
}
}
JabxbTest
package com.oner.jaxb.test;
import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.helpers.DefaultHandler;
import com.oner.jaxb.test.entities.Customer;
import com.oner.jaxb.test.entities.Customers;
import com.oner.jaxb.test.entities.PhoneNumber;
public class JaxbTest {
public static void main(String[] args) {
Customers customers = new Customers();
Customer customer1 = new Customer();
customer1.setName("123456");
customer1.getPhoneNumbers().add(new PhoneNumber());
customer1.getPhoneNumbers().add(new PhoneNumber());
customer1.getPhoneNumbers().add(new PhoneNumber());
customers.getCustomers().add(customer1);
Customer customer2 = new Customer();
customer2.setName("234");
customers.getCustomers().add(customer2);
try {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File("resources/customer.xsd"));
JAXBContext jc = JAXBContext.newInstance(Customers.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setSchema(schema);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
CustomerValidator customerValidator = new CustomerValidator();
marshaller.setEventHandler(customerValidator);
marshaller.marshal(customers, new DefaultHandler());
} catch (Exception e) {
e.printStackTrace();
}
}
}
customer.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="customers">
<xs:complexType>
<xs:sequence>
<xs:element ref="customer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="customer">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="stringMaxSize5"/>
<xs:element ref="phone-number" maxOccurs="2"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="phone-number">
<xs:complexType>
<xs:sequence/>
</xs:complexType>
</xs:element>
<xs:simpleType name="stringMaxSize5">
<xs:restriction base="xs:string">
<xs:maxLength value="5"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Now, here is the funny thing.
When I run the example using Java 1.6, I got below
cvc-maxLength-valid: Value '123456' with length = '6' is not facet-valid with respect to maxLength '5' for type 'stringMaxSize5'.
Customer [name=123456, phoneNumbers=[PhoneNumber [], PhoneNumber [], PhoneNumber []]]
cvc-type.3.1.3: The value '123456' of element 'name' is not valid.
Customer [name=123456, phoneNumbers=[PhoneNumber [], PhoneNumber [], PhoneNumber []]]
cvc-complex-type.2.4.d: Invalid content was found starting with element 'customer'. No child element '{phone-number}' is expected at this point.
Customers [customers=[Customer [name=123456, phoneNumbers=[PhoneNumber [], PhoneNumber [], PhoneNumber []]], Customer [name=234, phoneNumbers=[]]]]
cvc-complex-type.2.4.d: Invalid content was found starting with element 'customer'. No child element is expected at this point.
Customer [name=234, phoneNumbers=[]]
cvc-complex-type.2.4.b: The content of element 'customer' is not complete. One of '{phone-number}' is expected.
Customers [customers=[Customer [name=123456, phoneNumbers=[PhoneNumber [], PhoneNumber [], PhoneNumber []]], Customer [name=234, phoneNumbers=[]]]]
And with Java 1.7
cvc-maxLength-valid: Value '123456' with length = '6' is not facet-valid with respect to maxLength '5' for type 'stringMaxSize5'.
Customer [name=123456, phoneNumbers=[PhoneNumber [], PhoneNumber [], PhoneNumber []]]
cvc-type.3.1.3: The value '123456' of element 'name' is not valid.
Customer [name=123456, phoneNumbers=[PhoneNumber [], PhoneNumber [], PhoneNumber []]]
cvc-complex-type.2.4.d: Invalid content was found starting with element 'phone-number'. No child element is expected at this point.
PhoneNumber []
cvc-complex-type.2.4.d: Invalid content was found starting with element 'customer'. No child element is expected at this point.
Customer [name=234, phoneNumbers=[]]
cvc-complex-type.2.4.b: The content of element 'customer' is not complete. One of '{phone-number}' is expected.
Customers [customers=[Customer [name=123456, phoneNumbers=[PhoneNumber [], PhoneNumber [], PhoneNumber []]], Customer [name=234, phoneNumbers=[]]]]
My qustion and my expectations:
- I see 2 errors for first Customer objects, both regarding the Name field length, but why do I have get 2 validation errors ??
- "No child element is expected at this point" error is raised for second Customer but it shoud be the first, that has 3 PhoneNumber element, which should be max. 2
- "One of '{phone-number}' is expected" error is raised for Customers element, which is maybe acceptable, but I also expect this error for seconds Customer element, which has no PhoneNumber element where min. is 1
- Java 7 raised error for missing PhoneNumber on second customer, which is good, but it locates the honeNumber object is the Customer. I expect to see the Customer element also.
Can someone tell me what's going on ? Is ValidationEventLocator works correct or did I expect too much from JaxB?
Thanks