Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate Java objects with Bean Validation annotations from an XSD?

I'm writing an EJB as a contract first SOAP service and I generate the java classes and SEI from the WSDL. The WSDL specifies several types with constraints (max length, pattern, etc). The generated java classes are JAXB annotated but lack the contraints metadata because the JAXB annotations don't support those. This means that input validation only occurs when the service is called through the SOAP endpoint.

The problem is that when the EJB is called by another EJB the validation is bypassed since it is located in the XML stack. I would like to disable XML Schemavalidation and use Bean Validation instead so validation works for both ways (SOAP and RMI) of calling the EJB.

Question: How can I generate not only JAXB annotations but also Bean Validation annotations on the Java classes?

like image 876
Geert Schuring Avatar asked Nov 04 '11 14:11

Geert Schuring


People also ask

How do I generate Java classes from XSD using JAXB in Intellij?

Generate a Java class from an XML Schema using JAXB In the active editor tab, open the desired Schema . xsd file or an XML document, which contains the desired Schema. In the main menu, go to Tools | XML Actions | Generate Java Code From XML Schema Using JAXB.

How XSD is used in Java?

xsd is the XML schema you will use as input to the JAXB binding compiler, and from which schema-derived JAXB Java classes will be generated. For the Customize Inline and Datatype Converter examples, this file contains inline binding customizations.

Which command line command is used to generate XML schemas from Java source files?

Use the JAXB schema compiler, xjc command to generate JAXB-annotated Java classes. The schema compiler is located in the app_server_root \bin\ directory. The schema compiler produces a set of packages containing Java source files and JAXB property files depending on the binding options used for compilation.

How does Java Bean Validation work?

The Bean Validation API is a Java specification which is used to apply constraints on object model via annotations. Here, we can validate a length, number, regular expression, etc. Apart from that, we can also provide custom validations. As Bean Validation API is just a specification, it requires an implementation.


1 Answers

You can use the javax.xml.valdation APIs to validation a domain model mapped with JAXB against an XML schema. An advantage of this approach is that you use the same validation rules (defined in the XML schema) for both of your use cases:

import java.io.File;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.util.JAXBSource;
import javax.xml.validation.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        Customer customer = new Customer();
        customer.setName("Jane Doe");
        customer.getPhoneNumbers().add(new PhoneNumber());
        customer.getPhoneNumbers().add(new PhoneNumber());
        customer.getPhoneNumbers().add(new PhoneNumber());

        JAXBContext jc = JAXBContext.newInstance(Customer.class);
        JAXBSource source = new JAXBSource(jc, customer);

        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(new File("customer.xsd"));

        Validator validator = schema.newValidator();
        validator.setErrorHandler(new MyErrorHandler());
        validator.validate(source);
    }

}

Full Example

  • http://blog.bdoughan.com/2010/11/validate-jaxb-object-model-with-xml.html
like image 156
bdoughan Avatar answered Nov 14 '22 23:11

bdoughan