简体   繁体   English

Java:无法封装类型“entities.Person”作为元素因为

[英]Java : unable to marshal type “entities.Person” as an element because

In my webservice (using RestEasy) project I have a dependency to a jar. 在我的webservice(使用RestEasy)项目中,我对jar有依赖。 I use from that jar a java class : Person. 我从那个jar中使用了一个java类:Person。

My problem is that I need to serialize a Person instance to XML, But I get the following error : 我的问题是我需要将Person实例序列化为XML,但是我收到以下错误:

unable to marshal type "entities.Person" as an element because it is missing an @XmlRootElement annotation

However, I cannot change the Person class to add the annotation @XmlRootElement (it is a third party jar). 但是,我无法更改Person类以添加注释@XmlRootElement(它是第三方jar)。

Is there any other way (methods, libraries, ... ) to marshal a Person instance into XML without annotating Person class? 有没有其他方法(方法,库,...)将Person实例编组为XML而不注释Person类?

BTW, here it is the code I use, but it fails because of the missing annotation : 顺便说一句,这是我使用的代码,但由于缺少注释而失败:

String result;
Person person = personManager.findByPersonId(personId);
StringWriter sw = new StringWriter();
JAXBContext personContext = JAXBContext.newInstance(Person.class);
Marshaller personMarshaller = personContext.createMarshaller();
personMarshaller.marshal(person, sw);
result = sw.toString();
return Response.status(200).entity(result).build();

Thank you a lot. 非常感谢。

JAXB (JSR-222) implementations do not require any annotations be added to your domain model. JAXB(JSR-222)实现不需要将任何注释添加到域模型中。 In the absence of an @XmlRootElement (or @XmlElementDecl ) you simply need to wrap your root object in an instance of JAXBElement. 如果没有@XmlRootElement (或@XmlElementDecl ),您只需将根对象包装在JAXBElement的实例中。

JAXBElement<Person> jaxbElement = new JAXBElement<Person>(new QName("person"), Person.class, person);
personMarshaller.marshal(jaxbElement, sw);

For More Information 欲获得更多信息


Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group. 注意:我是EclipseLink JAXB(MOXy)的负责人,也是JAXB(JSR-222)专家组的成员。

If you need to provide metadata for your model, but don't have access to the source then MOXy offers an external metadata document extension you can use for this purpose. 如果您需要为模型提供元数据,但无法访问源,则MOXy会提供可用于此目的的外部元数据文档扩展。

Thank you, the link you gave me is useful, but I wonder if this EclipseLink library is safe to integrate in a commercial product (license)... 谢谢,你给我的链接很有用,但我想知道这个EclipseLink库是否可以安全地集成到商业产品中(许可证)......

MOXy has been the default JAXB implementation in WebLogic since version 12.1.1, so it's definitely enterprise ready. 从版本12.1.1开始,MOXy一直是WebLogic中的默认JAXB实现,所以它绝对是企业就绪的。

EclipseLink (and MOXy) is dual licensed under the Eclipse Public License and Eclipse Distribution License (BSD): EclipseLink(和MOXy)在Eclipse Public License和Eclipse Distribution License(BSD)下获得双重许可:

暂无
暂无

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

相关问题 JMS消息,由于缺少@XmlRootElement批注,因此无法将类型“ java.lang.String”作为元素封送 - JMS message, unable to marshal type “java.lang.String” as an element because it is missing an @XmlRootElement annotation 无法将类型“java.lang.Integer”编组为元素,因为它缺少 @XmlRootElement 注释 - unable to marshal type "java.lang.Integer" as an element because it is missing an @XmlRootElement annotation 无法将类型 [...] 编组为元素,因为它缺少 @XmlRootElement 注释 - unable to marshal type […] as an element because it is missing an @XmlRootElement annotation 无法将类型编组为XML元素,因为缺少@XmlRootElement注释 - Unable to marshal type as XML element because @XmlRootElement annotation is missing 无法将类型编组为元素,因为它缺少自动生成的类的 @XmlRootElement 注释 - unable to marshal type as an element because it is missing an @XmlRootElement annotation for auto generated classes 我有@xmlrootelement,但一直收到此异常:无法将类型编组为元素,因为它缺少@XmlRootElement - I have @xmlrootelement , but keep getting this exception: unable to marshal type as an element because it is missing an @XmlRootElement JAXB,Marshal的问题-无法封送类型“ java.lang.String” - Problems with JAXB, Marshal, - unable to marshal type “java.lang.String” JAXB,Marshal的问题, - 无法编组类型“java.lang.String” - Problems with JAXB, Marshal, - unable to marshal type “java.lang.String” 带有自定义JAX-B绑定的JAX-WS MarshalException:无法将类型“java.lang.String”编组为元素 - JAX-WS MarshalException with custom JAX-B bindings: Unable to marshal type “java.lang.String” as an element JAXB无法封送类型“ java.util.HashMap” - JAXB unable to marshal type “java.util.HashMap”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM