7

This is what I'm doing:

@XmlType(name = "foo")
@XmlAccessorType(XmlAccessType.NONE)
public final class Foo {
  @XmlElement(name = "title")
  public String title() {
    return "hello, world!";
  }
}

JAXB complains:

com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
JAXB annotation is placed on a method that is not a JAXB property
    this problem is related to the following location:
        at @javax.xml.bind.annotation.XmlElement(nillable=false, name=title, required=false, defaultValue=, type=class javax.xml.bind.annotation.XmlElement$DEFAULT, namespace=##default)
        at com.example.Foo

What to do? I don't want (and can't) rename the method.

2 Answers 2

5

There are a couple of different options:

Option #1 - Introduce a Field

If the value is constant as it is in your example, then you could introduce a field into your domain class and have JAXB map to that:

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

@XmlType(name = "foo")
@XmlAccessorType(XmlAccessType.NONE)
public final class Foo {
    @XmlElement
    private final String title = "hello, world!";

  public String title() {
    return title;
  }
}

Option #2 - Introduce a Property

If the value is calculated then you will need to introduce a JavaBean accessor and have JAXB map to that:

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

@XmlType(name = "foo")
@XmlAccessorType(XmlAccessType.NONE)
public final class Foo {

  public String title() {
    return "hello, world!";
  }

  @XmlElement
  public String getTitle() {
      return title();
  }

}
7
  • Than why do I need title() at all?
    – yegor256
    Commented Nov 3, 2011 at 15:00
  • @yegor256 - JAXB would not require the title() method.
    – bdoughan
    Commented Nov 3, 2011 at 15:05
  • But I require it :) I need title() to return the value. The solution proposed by Thor84no is the best at the moment, but I really hope that it's not the only one. Again, value has to be returned by title()
    – yegor256
    Commented Nov 3, 2011 at 16:48
  • @yegor256 - As shown in my code sample you can definitely have the title() method, it just isn't used by the JAXB implementation. Is the value returned from the title() method constant or calculated?
    – bdoughan
    Commented Nov 3, 2011 at 17:19
  • Since the value is calculated then you will need to introduce a get method as given by Thor84no. I would not introduce the field as is specified in that answer however. I have updated my answer with how it should look.
    – bdoughan
    Commented Nov 3, 2011 at 17:59
3

There might be a better way, but the first solution that comes to mind is:

@XmlElement(name = "title")
private String title;

public String getTitle() {
    return title();
}

Why is it you can't name your method according to Java conventions anyway?

3
  • Variable is redundant here, just @XmlElement-annotated getTitle() will be enough
    – yegor256
    Commented Nov 3, 2011 at 12:06
  • I wasn't sure, so I put it in just in case as that will definitely work.
    – Vala
    Commented Nov 3, 2011 at 12:39
  • +1 - When annotating a field (instance variable) that has a corresponding accessor you will want to be sure to set @XmlAccessorType(XmlAccessType.FIELD): blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html. Also in this case JAXB will apply a default mapping for the title property so the @XmlElement annotation is not required.
    – bdoughan
    Commented Nov 3, 2011 at 13:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.