tags:

views:

291

answers:

2

Hi!

I'm using JAXB with a CXF Web Service. When I pass:

<?xml version="1.0" ?><ns2:Optionen xmlns:ns2="http://test.at/ezustellung/global/20090501#"&gt;
<ns2:PdfAKonvertierung>true</ns2:PdfAKonvertierung><ns2:SignaturTyp>Adobe</ns2:SignaturTyp>
</ns2:Optionen>

to the Unmarshaller, the properties pdfAKonvertierung und signaturTyp are both null in my Object. But if I pass:

<?xml version="1.0" ?><ns2:Optionen xmlns:ns2="http://test.at/ezustellung/global/20090501#"&gt;
<PdfAKonvertierung>true</PdfAKonvertierung><SignaturTyp>Adobe</SignaturTyp>
</ns2:Optionen>

which is invalid, according to the CXF Validator and wikipedia (translation, haven't found that in english):

Child elements of an element with a namespace prefix do not automatically have the same namespace, they have to be prefixed with a namespace as well.

The properties are set accordingly. Can someone spot an error in my code, or have I found a bug in the JAXB implementation from Java 1.6?

For reference, here is my code:

 public class JaxbTests {
  @Test
  public void testOptionen() throws JAXBException, SAXException, IOException {
   JAXBContext context = JAXBContext.newInstance(Optionen.class);
   Unmarshaller unmarshaller = context.createUnmarshaller();
   ByteArrayInputStream is = new ByteArrayInputStream(
     // Does not work
     ("<?xml version=\"1.0\" ?><ns2:Optionen xmlns:ns2=\"http://test.at/ezustellung/global/20090501#\"&gt;" +
     "<ns2:PdfAKonvertierung>true</ns2:PdfAKonvertierung><ns2:SignaturTyp>Adobe</ns2:SignaturTyp>" +
     "</ns2:Optionen>").getBytes());

     // Works
 //    ("<?xml version=\"1.0\" ?><ns2:Optionen xmlns:ns2=\"http://test.at/ezustellung/global/20090501#\"&gt;" +
 //    "<PdfAKonvertierung>true</PdfAKonvertierung><SignaturTyp>Adobe</SignaturTyp>" +
 //    "</ns2:Optionen>").getBytes());
   Optionen opts = ((Optionen) unmarshaller.unmarshal(is));
   Assert.assertTrue(opts.isPdfAKonvertierung() == true);
   Assert.assertEquals(SignaturTypType.ADOBE, opts.getSignaturTyp());
  }
 }


 @XmlAccessorType(XmlAccessType.FIELD)
 @XmlType(name = "", propOrder = {
     "pdfAKonvertierung",
     "signaturTyp"
 })
 @XmlRootElement(name = "Optionen")
 public class Optionen {

     @XmlElement(name = "PdfAKonvertierung", defaultValue = "true")
     protected Boolean pdfAKonvertierung;
     @XmlElement(name = "SignaturTyp", defaultValue = "Adobe")
     protected SignaturTypType signaturTyp;

     public Optionen() {
      System.out.println("Optionen created");
     }
     public Boolean isPdfAKonvertierung() {
         return pdfAKonvertierung;
     }
     public void setPdfAKonvertierung(Boolean value) {
         this.pdfAKonvertierung = value;
     }
     public SignaturTypType getSignaturTyp() {
         return signaturTyp;
     }
     public void setSignaturTyp(SignaturTypType value) {
         this.signaturTyp = value;
     }

 }


 @XmlType(name = "SignaturTypType")
 @XmlEnum
 public enum SignaturTypType {

  @XmlEnumValue("Adobe")
  ADOBE("Adobe"), @XmlEnumValue("PDF-AS")
  PDF_AS("PDF-AS");
  private final String value;

  SignaturTypType(String v) {
   this.value = v;
  }

  public String value() {
   return this.value;
  }

  public static SignaturTypType fromValue(String v) {
   for (SignaturTypType c : SignaturTypType.values()) {
    if (c.value.equals(v)) {
     return c;
    }
   }
   throw new IllegalArgumentException(v);
  }

 }

package-info.java:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://test.at/ezustellung/global/20090501#")
package at.test.ezustellung.global._20090501_;
A: 

It's a little bit difficult to read your incorrectly formatted code, but wouldn't it fix your problem if you declare your elements with the correct namespace, e.g. @XmlElement(namespace = "http://test.at/ezustellung/global/20090501#", name = "SignaturTyp", defaultValue = "Adobe")?

jarnbjo
That solved the problem. Thanks a lot! Changing the following lines in the Optionen class, resulted in the unit test passing correctly: @XmlRootElement(namespace = "http://test.at/ezustellung/global/20090501#", name = "Optionen") public class Optionen { @XmlElement(namespace = "http://test.at/ezustellung/global/20090501#", name = "PdfAKonvertierung", defaultValue = "true") protected Boolean pdfAKonvertierung; @XmlElement(namespace = "http://test.at/ezustellung/global/20090501#", name = "SignaturTyp", defaultValue = "Adobe") protected SignaturTypType signaturTyp;
Florian
Then why don't you accept the answer or upvote it?
jarnbjo
Sorry, I'm new here, I have accepted your answer, but was unable to vote, as credits are required.
Florian
I can't get xjc to generate the namespace properties for the XmlElement / XmlRootElement annotations. Any ideas how to make that work?
Florian
+2  A: 

The solution (without modifying the generated classes) to the problem was to make sure that the

<xs:schema elementFormDefault="qualified">

attribute is present and to regenerate the jaxb mapping, so that

elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED

ends up in the package-info.java

Florian