简体   繁体   English

如何使用XSD中的Bean Validation注释生成Java对象?

[英]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. 我正在编写一个EJB作为契约第一个SOAP服务,我从WSDL生成java类和SEI。 The WSDL specifies several types with constraints (max length, pattern, etc). WSDL指定了几种带约束的类型(最大长度,模式等)。 The generated java classes are JAXB annotated but lack the contraints metadata because the JAXB annotations don't support those. 生成的java类是JAXB注释但缺少约束元数据,因为JAXB注释不支持这些。 This means that input validation only occurs when the service is called through the SOAP endpoint. 这意味着只有在通过SOAP端点调用服务时才会发生输入验证。

The problem is that when the EJB is called by another EJB the validation is bypassed since it is located in the XML stack. 问题是 ,当EJB被另一个EJB调用时,验证被绕过,因为它位于XML堆栈中。 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. 我想禁用XML Schemavalidation并使用Bean Validation,因此验证适用于调用EJB的两种方式(SOAP和RMI)。

Question : How can I generate not only JAXB annotations but also Bean Validation annotations on the Java classes? 问题 :如何在Java类上生成JAXB注释以及Bean Validation注释?

You can use the javax.xml.valdation APIs to validation a domain model mapped with JAXB against an XML schema. 您可以使用javax.xml.valdation API来验证针对XML模式使用JAXB映射的域模型。 An advantage of this approach is that you use the same validation rules (defined in the XML schema) for both of your use cases: 此方法的一个优点是您对两个用例使用相同的验证规则(在XML模式中定义):

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 完整的例子

我所知道的最佳答案是使用Annotate插件添加JSR 303注释。

您可以使用此插件从xsd生成Bean Validation注释https://github.com/krasa/krasa-jaxb-tools

You can use MOXy version 2.6+ as JAXB provider and this will be done automatically. 您可以使用MOXy 2.6+作为JAXB提供程序,这将自动完成。 MOXy is a framework module in EclipseLink project. MOXy是EclipseLink项目中的框架模块。

At the moment, there is version 2.6.0-M3 of EclipseLink available at: sonatype or maven . 目前,有一个版本2.6.0-M3的EclipseLink可用于: sonatypemaven

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM