Java 是否可以用非标准名称@XmlElement注释方法?

Java 是否可以用非标准名称@XmlElement注释方法?,java,jaxb,jaxb2,Java,Jaxb,Jaxb2,这就是我正在做的: @XmlType(name = "foo") @XmlAccessorType(XmlAccessType.NONE) public final class Foo { @XmlElement(name = "title") public String title() { return "hello, world!"; } } JAXB抱怨: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException:

这就是我正在做的:

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

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

怎么办?我不想(也不能)重命名该方法。

可能有更好的方法,但想到的第一个解决方案是:

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

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

为什么您不能根据Java约定命名方法?

有两种不同的选择:

选项#1-引入一个字段

如果该值与示例中的值相同,则可以在域类中引入一个字段,并让JAXB映射到该字段:

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;
  }
}
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();
  }

}
选项#2-引入一项属性

如果计算了该值,则需要引入JavaBean访问器,并使JAXB映射到该值:

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;
  }
}
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();
  }

}

变量在这里是多余的,只需
@xmlement
-注释的
getTitle()
就足够了,我不确定,所以我把它放进去以防万一,因为这肯定会起作用。+1-在注释具有相应访问器的字段(实例变量)时,您需要确保设置@xmlacessortype(xmlacesstype.field):。同样在这种情况下,JAXB将为title属性应用默认映射,因此不需要
@xmlement
注释。那么为什么我需要
title()
呢?@yegor256-JAXB不需要
title()
方法。但是我需要它:)我需要
title()
来返回值。Thor84no提出的解决方案目前是最好的,但我真的希望它不是唯一的。同样,值必须由
title()
@yegor256返回-如我的代码示例所示,您完全可以使用
title()
方法,它只是没有被JAXB实现使用。从
title()
方法返回的值是常量还是计算值?由于该值是计算值,因此需要引入一个由Thor84no给出的get方法。但是,我不会介绍该答案中指定的字段。我已经更新了我的答案,让它看起来如何。