gpt4 book ai didi

java - JaxB 验证事件定位器 - 错误的对象引用

转载 作者:太空宇宙 更新时间:2023-11-04 14:27:39 25 4
gpt4 key购买 nike

我只是想问以下问题:
奥 git _a
奥 git _a

我遇到了类似的问题,所以我尝试了这些解决方案,但我发现了一些奇怪的东西:

客户验证

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();
}

}

}

客户.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=[]]]]

我的问题和我的期望:

  1. 我看到第一个 Customer 对象出现 2 个错误,均与名称字段长度有关,但为什么我会出现 2 个验证错误?
  2. 第二个客户会出现“此时不需要子元素”错误,但它应该是第一个,它有 3 个 PhoneNumber 元素,最多应为 3 个。 2
  3. “Customers 元素出现“One of '{phone-number}' is Expected”错误,这可能是可以接受的,但我也预计此错误会持续几秒的 Customer 元素,该元素没有 PhoneNumber 元素,其中 min.是 1
  4. Java 7 引发了第二个客户缺少 PhoneNumber 的错误,这很好,但它发现 honeNumber 对象是 Customer。我希望也能看到 Customer 元素。

谁能告诉我发生了什么事吗? ValidationEventLocator 工作正常还是我对 JaxB 的期望过高?

谢谢

最佳答案

经过一番挖掘,我发现了一个有趣的插件,krasa-jaxb-tools,它添加了与 XSD Schame 验证规则等效的 JSR-303 注释。例子似乎很有希望:github repo

关于java - JaxB 验证事件定位器 - 错误的对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26502286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com