无法将类型编组为元素,因为它缺少自动生成的类的 @XmlRootElement 注释 [英] unable to marshal type as an element because it is missing an @XmlRootElement annotation for auto generated classes

查看:52
本文介绍了无法将类型编组为元素,因为它缺少自动生成的类的 @XmlRootElement 注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据我的模式验证 Class 对象,我在其中提供了正则表达式来验证自动生成的 JAXB 类的字段.当我尝试验证我的类对象时,出现以下错误:

I need to validate Class object against my schema in which I have provided regular expression to validate fields for auto generated JAXB classes. When I try to validate my class object I get below error:

无法编组类型xyz";作为元素,因为它缺少@XmlRootElement 注释

unable to marshal type "xyz" as an element because it is missing an @XmlRootElement annotation

这是我用来验证自动生成的类对象的代码:

Here is the code that I use to validate my autogenerated class object:

jc = JAXBContext.newInstance(obj.getClass());
source = new JAXBSource(jc, obj);
Schema schema = schemaInjector.getSchema();
Validator validator = schema.newValidator();
validator.validate(source);

有没有其他办法可以解决这个问题?

Is there any other way I can solve this?

推荐答案

如果您的类没有 @XmlRootElement 注释,那么您可以将其包装在 JAXBElement 的实例中>.如果您从 XML 模式生成类,那么生成的 ObjectFactory 可能有一个方便的方法.

If your class does not have an @XmlRootElement annotation then you can wrap it in an instance of JAXBElement. If you generated your classes from an XML Schema then the generated ObjectFactory may have a convenience method for you.

我在我的博客:

这篇关于无法将类型编组为元素,因为它缺少自动生成的类的 @XmlRootElement 注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆