@XmlElement是否可以使用非标准名称注释方法? [英] Is it possible to @XmlElement annotate a method with non-stardard name?

查看:103
本文介绍了@XmlElement是否可以使用非标准名称注释方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我正在做的事情:

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投诉:

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.

推荐答案

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

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

为什么你不能根据Java惯例命名你的方法呢?

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

这篇关于@XmlElement是否可以使用非标准名称注释方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆