如何从 XSD 生成带有 Bean Validation 注释的 Java 对象?

发布于 2024-12-13 14:16:09 字数 391 浏览 1 评论 0原文

我正在编写一个 EJB 作为契约优先 SOAP 服务,并从 WSDL 生成 java 类和 SEI。 WSDL 指定了几种带有约束的类型(最大长度、模式等)。生成的 java 类带有 JAXB 注释,但缺少约束元数据,因为 JAXB 注释不支持这些元数据。这意味着仅当通过 SOAP 端点调用服务时才会发生输入验证。

问题是,当 EJB 被另一个 EJB 调用时,验证会被绕过,因为它位于 XML 堆栈中。我想禁用 XML Schemavalidation 并改用 Bean Validation,以便验证适用于调用 EJB 的两种方式(SOAP 和 RMI)。

问题:如何在 Java 类上不仅生成 JAXB 注释,还生成 Bean Validation 注释?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

被你宠の有点坏 2024-12-20 14:16:09

您可以使用 javax.xml.valdation API 根据 XML 架构验证使用 JAXB 映射的域模型。这种方法的优点是您可以为两个用例使用相同的验证规则(在 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);
    }

}

完整示例

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

ら栖息 2024-12-20 14:16:09

到目前为止我知道的最好答案是使用 注释插件 添加 JSR 303 注释。

The best answer I know till now is to use the Annotate Plugin to add JSR 303 annotations.

花期渐远 2024-12-20 14:16:09

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

You can generate Bean Validation annotations from xsd using this plugin https://github.com/krasa/krasa-jaxb-tools

忘羡 2024-12-20 14:16:09

您可以使用 MOXy 版本 2.6+ 作为 JAXB 提供程序,这将自动完成。 MOXy是EclipseLink项目中的一个框架模块。

目前,EclipseLink 版本 2.6.0-M3 可用:
sonatype
maven

You can use MOXy version 2.6+ as JAXB provider and this will be done automatically. MOXy is a framework module in EclipseLink project.

At the moment, there is version 2.6.0-M3 of EclipseLink available at:
sonatype
or maven.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文