JAXB 2.1为生成的类实现Comparable <t>

时间:2016-08-12 05:22:34

标签: java jaxb2 maven-jaxb2-plugin jaxb2-basics

  • 使用Jaxb 2.1从.xsd
  • 生成java代码
  • 使用jaxb2-basics插件
  • 想要生成类Fragment来实现Comparable<Fragment>
public class Fragment implements Serializable, Comparable<Fragment> {
  ...
  public int compareTo(Fragment other) {
    .....
    return 0;
  }
}

使用以下jaxb绑定文件

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:ci="http://jaxb.dev.java.net/plugin/code-injector"
    xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
    jxb:extensionBindingPrefixes="xjc" 
    jxb:version="2.1">
    <jxb:bindings>
        <jxb:globalBindings>
            <xjc:serializable uid="12343" />
        </jxb:globalBindings>
    </jxb:bindings>
    <jxb:bindings schemaLocation="../schemas/Fragment.xsd"
        version="1.0" node="/xs:schema">
        <jxb:bindings node="//xs:element[@name='Fragment']/xs:complexType">         
            <inheritance:implements>java.lang.Comparable</inheritance:implements>
            <ci:code>           
public int compareTo(Fragment other) {

    return fragmentVersion.compareTo(other.fragmentVersion);
}
            </ci:code>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

能够生成如下所示的课程:

public class Fragment implements Serializable, Comparable {
  ...
  public int compareTo(Fragment other) {
    return fragmentVersion.compareTo(other.fragmentVersion);
  }
}

问题:如您所见,类生成为implements Comparable而不是implements Comparable<Fragment>

我确定,遗漏了一些东西。任何有待解决的帮助都表示赞赏。

2 个答案:

答案 0 :(得分:4)

在测试词典提供的解决方案时,注意到另一种解决问题的方法:

<inheritance:implements><![CDATA[java.lang.Comparable<Fragment>]]></inheritance:implements>

答案 1 :(得分:3)

JAXB2-Basics支持泛型。

只需使用:

<inheritance:implements>java.lang.Comparable&lt;Fragment&gt;</inheritance:imple‌​ments>