zibi
zibi

Reputation: 3313

JAXB, how to validate nillable and required field when unmarshalling

I have a small problem with JAXB, but unfortunately I was not able to find answer.

I have a class Customer, with 2 fields name and city, the mapping is done using annotations and both fields are marked as required and not nillable.

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

However, when I submit such XML file:

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

I will receive a Customer instance with both fields set to null.

Shouldn't there be a validation exception or am I missing something in the mapping?

To unmarshal I use:

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

Upvotes: 6

Views: 9529

Answers (2)

Paweł Gutowski
Paweł Gutowski

Reputation: 41

You may automagically generate schema in runtime and use it for validation. This will do the job:

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

Upvotes: 4

dkaustubh
dkaustubh

Reputation: 454

You need to use the schema to validate. JAXB can't do validation on its own.

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

Upvotes: 7

Related Questions