Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JAXB,解组时如何验证nillable和required字段_Java_Xml_Jaxb - Fatal编程技术网

Java JAXB,解组时如何验证nillable和required字段

Java JAXB,解组时如何验证nillable和required字段,java,xml,jaxb,Java,Xml,Jaxb,我对JAXB有一个小问题,但不幸的是,我无法找到答案 我有一个类Customer,有两个字段name和city,映射是使用注释完成的,这两个字段都标记为必需的,不可为零 @XmlRootElement(name = "customer") public class Customer { enum City { PARIS, LONDON, WARSAW } @XmlElement(name = "name", required = true, nill

我对JAXB有一个小问题,但不幸的是,我无法找到答案

我有一个类Customer,有两个字段namecity,映射是使用注释完成的,这两个字段都标记为必需的,不可为零

@XmlRootElement(name = "customer")
public class Customer {

    enum City {
        PARIS, LONDON, WARSAW
    }

    @XmlElement(name = "name", required = true, nillable = false)
    public String name;
    @XmlElement(name = "city", required = true, nillable = false)
    public City city;

    @Override
    public String toString(){
        return String.format("Name %s, city %s", name, city);
    }
}
但是,当我提交此类XML文件时:

<customer>
    <city>UNKNOWN</city>
</customer>

您需要使用模式进行验证。JAXB不能自己进行验证

SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(ClassUtils.getDefaultClassLoader().getResource(schemaPath));
unmarshaller.setSchema(schema);

您可以在运行时自动生成模式并将其用于验证。 这将完成以下工作:

JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
generateAndSetSchema(unmarshaller);
Customer customer = (Customer) unmarshaller.unmarshal(in);

private void generateAndSetSchema(Unmarshaller unmarshaller) {

    // generate schema
    ByteArrayStreamOutputResolver schemaOutput = new ByteArrayStreamOutputResolver();
    jaxbContext.generateSchema(schemaOutput);

    // load schema
    ByteArrayInputStream schemaInputStream = new ByteArrayInputStream(schemaOutput.getSchemaContent());
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new StreamSource(schemaInputStream));

    // set schema on unmarshaller
    unmarshaller.setSchema(schema);
}

private class ByteArrayStreamOutputResolver extends SchemaOutputResolver {

    private ByteArrayOutputStream schemaOutputStream;

    public Result createOutput(String namespaceURI, String suggestedFileName) throws IOException {

        schemaOutputStream = new ByteArrayOutputStream(INITIAL_SCHEMA_BUFFER_SIZE);
        StreamResult result = new StreamResult(schemaOutputStream);

        // We generate single XSD, so generator will not use systemId property
        // Nevertheless, it validates if it's not null.
        result.setSystemId("");

        return result;
    }

    public byte[] getSchemaContent() {
        return schemaOutputStream.toByteArray();
    }
}
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
generateAndSetSchema(unmarshaller);
Customer customer = (Customer) unmarshaller.unmarshal(in);

private void generateAndSetSchema(Unmarshaller unmarshaller) {

    // generate schema
    ByteArrayStreamOutputResolver schemaOutput = new ByteArrayStreamOutputResolver();
    jaxbContext.generateSchema(schemaOutput);

    // load schema
    ByteArrayInputStream schemaInputStream = new ByteArrayInputStream(schemaOutput.getSchemaContent());
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new StreamSource(schemaInputStream));

    // set schema on unmarshaller
    unmarshaller.setSchema(schema);
}

private class ByteArrayStreamOutputResolver extends SchemaOutputResolver {

    private ByteArrayOutputStream schemaOutputStream;

    public Result createOutput(String namespaceURI, String suggestedFileName) throws IOException {

        schemaOutputStream = new ByteArrayOutputStream(INITIAL_SCHEMA_BUFFER_SIZE);
        StreamResult result = new StreamResult(schemaOutputStream);

        // We generate single XSD, so generator will not use systemId property
        // Nevertheless, it validates if it's not null.
        result.setSystemId("");

        return result;
    }

    public byte[] getSchemaContent() {
        return schemaOutputStream.toByteArray();
    }
}