Is it possible to @XmlElement annotate a method with non-stardard name?
Asked Answered
S

2

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.

Schrock answered 3/11, 2011 at 12:0 Comment(0)
H
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?

Hathorn answered 3/11, 2011 at 12:4 Comment(3)
Variable is redundant here, just @XmlElement-annotated getTitle() will be enoughSchrock
I wasn't sure, so I put it in just in case as that will definitely work.Hathorn
+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.Mike
M
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();
  }

}
Mike answered 3/11, 2011 at 14:35 Comment(7)
Than why do I need title() at all?Schrock
@Schrock - JAXB would not require the title() method.Mike
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()Schrock
@Schrock - 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?Mike
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.Mike
Yes, I understand, thanks. This is what Thor84no suggested already. If this is the only solution I will select his answer, since he was the first.Schrock
This does not appear to work without a corresponding "setTitle(String)" method also defined. Even if you use @XmlWriteOnly with Moxy. It simply ignores the field.Adelaidaadelaide
H
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?

Hathorn answered 3/11, 2011 at 12:4 Comment(3)
Variable is redundant here, just @XmlElement-annotated getTitle() will be enoughSchrock
I wasn't sure, so I put it in just in case as that will definitely work.Hathorn
+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.Mike

© 2022 - 2024 — McMap. All rights reserved.