Why doesn't JAXB allow annotations on getters that all pull from the same member variable?

Why does Example A work, while Example B throws a "JAXB note is thrown in an exception?"?

I am using JAX-WS with Spring MVC.

Example A

package com.casanosa2.permissions;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType(name = "FooXMLMapper")
public class FooXMLMapper implements IFoo {

 @XmlElement
 private final boolean propA;

 @XmlElement
 private final boolean propB;

 public FooMapper(IFoo foo) {
  propA = foo.getPropA()
  propB = foo.getPropB()
 }

 public FooMapper() {
  propA = false;
  propB = false;
 }

 @Override
 public boolean getPropA() {
  return propA;
 }

 @Override
 public boolean getPropB() {
  return propB;
 }
}

Example B

@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType(name = "FooXMLMapper")
public class FooXMLMapper {

 private final IFoo foo;

 public FooMapper() {
  foo = new IFoo() {

   @Override
   public boolean getPropA() {
    return false;
   }

   @Override
   public boolean getPropB() {
    return false;
   }

  };
 }

 public FooXMLMapper(IFoo foo) {
  this.foo = foo;
 }

 @XmlElement
 public boolean getPropA() {
  return foo.getPropA();
 }

 @XmlElement
 public boolean getPropB() {
  return foo.getPropB();
 }
}
+5
source share
3 answers

I believe accessors are ignored if they look directly at instance variables, and in your example B there are no actual instance variables of the correct name. You must explicitly tell it that @XmlAccessorType (XmlAccessType.NONE) is for the class and @XmlElement and @XmlAttribute in the get / set methods. At least this is what I ended up with JAXB mapping.

+5

, A, , B. A ( get/set), ( ).

+2

I believe this will be a proper JAXB property, you need setters for them, as well as getters. (you will also need a default constructor).

+1
source