1

Tengo un problema tratando de construir un CDATA-XML partiendo de un bean creado, la idea del extends es que cualquier bean pueda ser convertido ya que existiran varias "plantillas" este es el código:

Bean: PlantillaGenerica

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: Plantilla

@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;
  }

}

Metodo para hacer el 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;
}

Excepcion:

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

Agradezco su ayuda....

1
  • "PlantillaGenerica" as an element because it is missing an @XmlRootElement annotationosea no se puede porque a PantillaGenerica le falta la anotación @XMLRootElement, lo dice tu error el 17 dic. 2018 a las 19:02

1 respuesta 1

1

La definición de @XmlRootElement es:

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

por lo que las subclases de una clase anotada con @XmlRootElement no "heredarán" la anotación; para que eso fuera así debería estar marcada como @Inherited. De su javadoc:

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

En resumen: añade @XmlRootElement a PlantillaGenerica

1
  • Gracias por tu respuesta, tal como lo indicaste solo hacia falta añadir la etiqueta a la clase hija.
    – EdwinCab
    el 17 abr. 2017 a las 14:33

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.