0 Replies Latest reply on Apr 20, 2006 8:49 PM by Wayne Hannam

    JAXB 2.0 Marshaling Exception

    Wayne Hannam Newbie

      When I attempt to run the following code, I get:

      JAXBException: org.xml.sax.SAXException: unable to marshal type "com.parasynthion.centurion.hibernate.entity.Testjaxb" as an element because it is missing an @XmlRootElement annotationnull

      I have included the XSD, generated class from XJC, and the servlet code that is attempting to use it.

      The same class file, generated from the same XSD, with a standalone running class, works perfectly.

      I am running 4.0.4.CR2, but the same thing happens under 4.0.3SP1

      Any help will be greatly appreciated.


      XSD:

      <?xml version="1.0" encoding="UTF-8"?>
      <xs:schema xmlnss="http://www.w3.org/2001/XMLSchema"
      targetNamespace="com.parasynthion.centurion.hibernate.entity"
      elementFormDefault="qualified"
      xmlns="com.parasynthion.centurion.hibernate.entity"
      xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
      jaxb:version="1.0">

      <xs:element name="testjaxb">
      <xs:complexType>
      <xs:sequence>
      <xs:element name="foo" type="xs:integer"/>
      </xs:sequence>
      </xs:complexType>
      </xs:element>
      </xs:schema>


      Generated Class:

      //
      // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.0-b26-ea3
      // See http://java.sun.com/xml/jaxb
      // Any modifications to this file will be lost upon recompilation of the source schema.
      // Generated on: 2006.04.20 at 05:18:40 PM MDT
      //


      package com.parasynthion.centurion.hibernate.entity;

      import java.math.BigInteger;
      import javax.xml.bind.annotation.AccessType;
      import javax.xml.bind.annotation.XmlAccessorType;
      import javax.xml.bind.annotation.XmlElement;
      import javax.xml.bind.annotation.XmlRootElement;
      import javax.xml.bind.annotation.XmlType;
      import com.parasynthion.centurion.hibernate.entity.Testjaxb;


      /**
      * Java class for testjaxb element declaration.
      *
      * The following schema fragment specifies the expected content contained within this class.
      *
      *
      *
      *
      *
      *
      *
      *
      *
      *
      *
      *
      *
      *
      *
      *
      */
      @XmlAccessorType(AccessType.FIELD)
      @XmlType(name = "", propOrder = {
      "foo"
      })
      @XmlRootElement(name = "testjaxb")
      public class Testjaxb {

      @XmlElement(namespace = "com.parasynthion.centurion.hibernate.entity")
      protected BigInteger foo;

      /**
      * Gets the value of the foo property.
      *
      * @return
      * possible object is
      * {@link BigInteger }
      *
      */
      public BigInteger getFoo() {
      return foo;
      }

      /**
      * Sets the value of the foo property.
      *
      * @param value
      * allowed object is
      * {@link BigInteger }
      *
      */
      public void setFoo(BigInteger value) {
      this.foo = value;
      }

      public boolean isSetFoo() {
      return (this.foo!= null);
      }

      }


      Servlet Code:

      package com.parasynthion.centurion.web;

      import java.io.*;
      import java.util.*;
      import java.math.*;
      import java.lang.*;
      import java.sql.*;
      import javax.servlet.*;
      import javax.servlet.http.*;
      import javax.naming.*;
      import org.hibernate.*;
      import com.parasynthion.centurion.hibernate.entity.*;
      import javax.xml.bind.JAXBContext;
      import javax.xml.bind.Unmarshaller;
      import javax.xml.bind.Marshaller;
      import javax.xml.bind.UnmarshalException;
      import javax.xml.bind.MarshalException;
      import javax.xml.bind.JAXBException;
      import javax.xml.validation.*;
      import org.xml.sax.*;
      import javax.xml.datatype.*;

      public class TestHibernateEventServlet extends HttpServlet
      {
      public void service(HttpServletRequest request,HttpServletResponse response)
      throws IOException,ServletException
      {
      PrintWriter out=response.getWriter();
      response.setContentType("text/xml");

      try
      {
      Testjaxb tj=new Testjaxb();
      tj.setFoo(new BigInteger("1"));

      JAXBContext jaxbContext=JAXBContext.newInstance(com.parasynthion.centurion.hibernate.entity.Testjaxb.class);
      Marshaller marshaller=jaxbContext.createMarshaller();
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT ,new Boolean(true));
      marshaller.marshal(tj,out);

      }
      catch(UnmarshalException ue)
      {
      sendError(out,ue,"UnmarshalException: ");
      ue.printStackTrace();
      }
      catch(IllegalArgumentException iae)
      {
      iae.printStackTrace();
      }
      catch(JAXBException jaxbe)
      {
      sendError(out,jaxbe,"JAXBException: ");
      jaxbe.printStackTrace();
      }
      /*catch(NamingException ne)
      {
      ne.printStackTrace();
      }*/
      }

      private void sendError(PrintWriter out,Exception e,String message)
      {
      out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
      out.print("");
      out.print(""+message+"");
      out.print(""+e.getCause()+"");
      out.print("<exception-message>"+e.getMessage()+"</exception-message>");
      out.print("");
      }
      }