Java JaxB验证事件定位器-错误的对象引用
我只是想问以下问题:Java JaxB验证事件定位器-错误的对象引用,java,xml,validation,jaxb,Java,Xml,Validation,Jaxb,我只是想问以下问题: 我也有类似的问题,所以我尝试了这些解决方案,但我发现了一些奇怪的问题: 客户验证 package com.oner.jaxb.test; import javax.xml.bind.ValidationEvent; import javax.xml.bind.ValidationEventHandler; public class CustomerValidator implements ValidationEventHandler { @Override
我也有类似的问题,所以我尝试了这些解决方案,但我发现了一些奇怪的问题: 客户验证
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 + "]";
}
}
jabxtest
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>
使用Java1.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=[]]]]
我的问题和期望:
谢谢经过一番挖掘,我发现了一个有趣的插件krasa jaxb tools,它添加了JSR-303注释,相当于XSD Schame验证规则。示例似乎很有希望:我认为您对JAXB的期望太高了。模式和模式派生类之间有很好的距离。有时候,你的模型会被扭曲,以至于你几乎认不出来。我不会依赖模式验证来推断对象结构中的错误。不过,这是一个很好的问题。好吧,这就是我目前正在做的事情,但不知何故,我希望像javax.validation一样使用JaxB验证,但这似乎是不可能的。我想我最好的选择是使用javax.validation注释对这些字段进行注释并对其进行验证。但这也意味着双重工作+每次模式规则更改时,我也需要将其应用于验证规则。如果这有帮助(不确定),您可以使用我的
jaxb2注释插件
,它可以向模式派生类添加任意注释。@lexicore,我发现了另一个有趣的插件,krasa jaxb tools,它添加了JSR-303注释,相当于XSD Schame验证规则。有趣。请考虑就此问题提出答案。
<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>
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=[]]]]
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=[]]]]