我只是想看下面的问题:
How to get validation events with JaXB?
Validate nested object from complex object using jaxb
我有类似的问题,所以我尝试了这些解决方案,但我发现了一些奇怪的事情:
客户验证
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;
}
}
客户
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 + "]";
}
}
客户
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 + "]";
}
}
******中国
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>
现在,这是有趣的事情。
当我使用Java 1.6运行该示例时,我得到了
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=[]]]]
使用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=[]]]]
我的气魄和我的期望:
有人能告诉我发生了什么事吗? ValidationEventLocator是否正常工作,或者我对JaxB的期望是否过高?
由于
答案 0 :(得分:0)
经过一番挖掘后,我找到了一个有趣的插件 krasa-jaxb-tools ,它添加了JSR-303注释等效的XSD Schame Validation规则。示例似乎有问题:github repo