Exception using Jaxb to build CDATA from my own bean

1

I have a problem trying to build a CDATA-XML from a created bean, the idea of the extends is that any bean can be converted since there will be several "templates" this is the code:

Bean: TemplateGenerica

public class PlantillaGenerica extends Plantilla{

  @XmlElement(name="Asunto")
  private String Asunto;

  @XmlElement(name="Cuerpo")
  private String Cuerpo;

  public String getAsunto() {
    return Asunto;
  }
  public void setAsunto(String asunto) {
    Asunto = asunto;
  }
  public String getCuerpo() {
    return Cuerpo;
  }
  public void setCuerpo(String cuerpo) {
    Cuerpo = cuerpo;
  } 
}

Bean: Template

@XmlRootElement(name="Correo")
public abstract class Plantilla {

  private String producto;
  private String evento;
  private String transaccion;
  private String fecha;
  public String getProducto() {
    return producto;
  }
  public void setProducto(String producto) {
    this.producto = producto;
  }
  public String getEvento() {
    return evento;
  }
  public void setEvento(String evento) {
    this.evento = evento;
  }
  public String getTransaccion() {
    return transaccion;
  }
  public void setTransaccion(String transaccion) {
    this.transaccion = transaccion;
  }
  public String getFecha() {
    return fecha;
  }
  public void setFecha(String fecha) {
    this.fecha = fecha;
  }

}

Method to do the marshall:

public String templateToString(Plantilla correo){
    String cdataStr="";
    JAXBContext jaxbContext;
    StringWriter sw = new StringWriter();

    try {
        jaxbContext = JAXBContext.newInstance(correo.getClass());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.marshal(correo, sw);
        CDATA cdata = DocumentHelper.createCDATA(sw.toString());
        cdataStr = cdata.asXML();
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return cdataStr;
}

Exception:

javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "com.mercurytfs.mercury.customers.banorte.integration.mca.service.impl.notificador.cdata.PlantillaGenerica" as an element because it is missing an @XmlRootElement annotation]
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:311)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:236)
    at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:116)
    at com.mercurytfs.mercury.customers.banorte.integration.mca.service.impl.notificador.NotificadorServiceImpl.templateToString(NotificadorServiceImpl.java:92)
    at com.mercurytfs.mercury.customers.banorte.modules.notification.business.aop.NotificationBanorteAOP.makeRequestForSendMailUsingAltamira(NotificationBanorteAOP.java:212)
    at com.mercurytfs.mercury.customers.banorte.modules.notification.business.aop.NotificationBanorteAOP.sendEmail(NotificationBanorteAOP.java:134)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:629)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:618)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
    at com.sun.proxy.$Proxy234.sendNotification(Unknown Source)
    at com.mercurytfs.mercury.modules.notification.service.impl.db.jobs.WorkflowEmailNotificationsJob.executeInternal(WorkflowEmailNotificationsJob.java:80)
    at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:75)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: com.sun.istack.internal.SAXException2: unable to marshal type "com.mercurytfs.mercury.customers.banorte.integration.mca.service.impl.notificador.cdata.PlantillaGenerica" as an element because it is missing an @XmlRootElement annotation
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:237)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:322)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:483)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:308)
    ... 25 more

I appreciate your help ....

    
asked by MrDev 16.04.2017 в 17:34
source

1 answer

1

The definition of @XmlRootElement is:

@Retention(value=RUNTIME)
@Target(value=TYPE)
public @interface XmlRootElement

so the subclasses of a class annotated with @XmlRootElement will not "inherit" the annotation; for that to be the case it should be marked as @Inherited . From your javadoc:

  

Indicates that an annotation type is automatically inherited. If an inherited meta-annotation is presented on an annotation type declaration, and the user queries the annotation type on a class declaration, and the class declaration has not annotated for this type, then the class's superclass will automatically be queried for the annotation type.

In summary: add @XmlRootElement to PlantillaGenerica

    
answered by 16.04.2017 / 18:22
source