编程语言
首页 > 编程语言> > java – JAXB 2.1为生成的Class实现Comparable

java – JAXB 2.1为生成的Class实现Comparable

作者:互联网

>使用Jaxb 2.1从.xsd生成java代码
>使用jaxb2-basics插件
>想要生成Class 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);
  }
}

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

当然,缺少一些东西.任何有待解决的帮助表示赞赏.

解决方法:

JAXB2-Basics支持泛型.

只需使用:

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

标签:maven-jaxb2-plugin,jaxb2,java,jaxb2-basics
来源: https://codeday.me/bug/20190727/1554269.html