Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 annotation is placed on a method that is not a JAXB property" exception?

I'm 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();
 }
}
like image 813
Bernard Igiri Avatar asked Dec 15 '10 16:12

Bernard Igiri


People also ask

What is @XmlTransient?

The @XmlTransient annotation is useful for resolving name collisions between a JavaBean property name and a field name or preventing the mapping of a field/property. A name collision can occur when the decapitalized JavaBean property name and a field name are the same.

What is @XmlRootElement?

Annotation Type XmlRootElementMaps a class or an enum type to an XML element. Usage. The @XmlRootElement annotation can be used with the following program elements: a top level class. an enum type.

What is XmlAccessorType?

Annotation Type XmlAccessorTypeControls whether fields or Javabean properties are serialized by default. Usage. @XmlAccessorType annotation can be used with the following program elements: package. a top level class.


1 Answers

I believe the accessors are ignored if it's looking directly at the instance variables and in your example B there are no actual instance variables of the right name. You have to tell it explicitly to use @XmlAccessorType(XmlAccessType.NONE) on the class and @XmlElement and @XmlAttribute on the get/set methods. At least, that's what I ended up doing with my JAXB mapping.

like image 89
Chris Kessel Avatar answered Sep 28 '22 04:09

Chris Kessel