title: I have @xmlrootelement , but keep getting this exception: unable to marshal type as an element because it is missing an @XmlRootElement
body: <p>I am trying to <code>marshall/unmarshall</code> a class which does not have <code>@xmlrootelement</code>. Can anyone help ? The marshaller I am using is of type Jaxb2Marshaller of the Spring's <code>org.springframework.oxm.jaxb</code>  Most of the solutions present are of type <code>javax.xml.bind</code> <code>Marshaller</code> and <code>JAXBContext</code>. Need help in <code>marshalling</code> a Java Class which does not have any <code>@xmlrootelement</code> provided, generated from XML using Spring's <code>JAXB2MARSHALLER</code>.</p>
  
  <p>I am not able to add the <code>@xmlrootelement</code> because this is an external <code>.jar</code></p>
  
  <p>I have tried all these solutions which actually provide a way of marshalling/unmarshalling using javax instead of spring</p>
  
  <p><a href="https://stackoverflow.com/questions/24519449/unable-to-marshal-type-as-an-element-because-it-is-missing-an-xmlrootelement-an">unable to marshal type as an element because it is missing an @XmlRootElement annotation for auto generated classes</a></p>
  
  <p><a href="https://stackoverflow.com/questions/37050316/unable-to-marshal-type-as-xml-element-because-xmlrootelement-annotation-is-miss/37050473">Unable to marshal type as XML element because @XmlRootElement annotation is missing</a></p>
  
  <p>Including this which has a very neat explanation but uses Marshaller instead of JaxB2Marshaller</p>
  
  <p><a href="https://howtodoinjava.com/jaxb/marshal-without-xmlrootelement/" rel="nofollow noreferrer">https://howtodoinjava.com/jaxb/marshal-without-xmlrootelement/</a></p>
  
  <p>Class need to undergo Marshalling without <code>@xmlrootelement</code></p>
  
  <p>PartnerRequest.java class which has <code>@xmlrootelement</code></p>
  
  <pre><code>@XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(
      name = "",
      propOrder = {"partnerCdId", "clientProductCode"}
  )
  @XmlRootElement(
      name = "PartnerRequest"
  )
  public class PartnerRequest
  
  
  
      @XmlAccessorType(XmlAccessType.FIELD)
      @XmlType(
          name = "Partner",
         )
      public class Partner{
      }
  </code></pre>
  
  <p>Exception I am getting is as below:</p>
  
  <pre><code>[com.sun.istack.internal.SAXException2: unable to marshal type "com.svc.partner.v1.PartnerRequest" as an element because it is missing an @XmlRootElement annotation]
      at org.springframework.oxm.jaxb.Jaxb2Marshaller.convertJaxbException(Jaxb2Marshaller.java:933)
      at org.springframework.oxm.jaxb.Jaxb2Marshaller.marshal(Jaxb2Marshaller.java:709)
      at org.springframework.ws.support.MarshallingUtils.marshal(MarshallingUtils.java:81)
      at org.springframework.ws.client.core.WebServiceTemplate$2.doWithMessage(WebServiceTemplate.java:395)
      at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:573)
      at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:539)
      at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:386)
      at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:380)
      at com.vmd.services.BaseService.marshallSendAndReceive(BaseService.java:53)
      at com.vmd.services.PartnerServiceImpl.getPartner(PartnerServiceImpl.java:27)
      at com.vmd.services.PartnerServiceImplTest.ShouldGetPartner(PartnerServiceImplTest.java:36)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
      at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
      at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
      at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
      at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
      at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
      at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
      at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
      at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  Caused by: javax.xml.bind.MarshalException: null
      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 org.springframework.oxm.jaxb.Jaxb2Marshaller.marshal(Jaxb2Marshaller.java:705)
      ... 39 common frames omitted
  Caused by: com.sun.istack.internal.SAXException2: unable to marshal type "com.svc.partner.v1.PartnerRequest" as an element because it is missing an @XmlRootElement annotation
      at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:234)
      at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:323)
      at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:479)
      at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:308)
      ... 41 common frames omitted
  </code></pre>
  
  <p>Also, Remember we need to Marshall it using Jaxb2Mashaller not Marshaller. This is an external jar/dependency not editable code.</p>
  
  <p>Thanks in Advance.</p>