2011-07-06 26 views
2

我有这个模式,我使用JAXB生成java存根文件。JAXB绑定 - 定义列表的返回类型<T>方法

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:c="http://www.a.com/f/models/types/common" 
    targetNamespace="http://www.a.com/f/models/types/common" 
    elementFormDefault="qualified"> 

    <xs:complexType name="constants"> 
     <xs:sequence> 
      <xs:element name="constant" type="c:constant" maxOccurs="unbounded"/> 
     </xs:sequence> 
    </xs:complexType> 

    <xs:complexType name="constant"> 
     <xs:sequence> 
      <xs:element name="reference" type="c:reference"/> 
     </xs:sequence> 
     <xs:attribute name="name" use="required" type="xs:string"/> 
     <xs:attribute name="type" use="required" type="c:data-type"/> 
    </xs:complexType> 

默认的Java包的名称是“com.afmodels.types.common”

我也有“常量”和“常量”定义的包“com.afmodel.common现有的接口'我想要生成的类使用哪个 。我使用的JAXB绑定文件,以确保生成的Java类实现 所需的接口

<jxb:bindings schemaLocation="./commonmodel.xsd" node="/xs:schema"> 
    <jxb:bindings node="xs:complexType[@name='constants']"> 
     <jxb:class/> 
     <inheritance:implements>com.a.f.model.common.Constants</inheritance:implements> 
    </jxb:bindings> 

下生成的类并实现了正确的接口

package com.a.f.models.types.common; 
.. 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "constants", propOrder = { 
    "constant" 
}) 
public class Constants 
    implements com.a.f.model.common.Constants 
{ 

    @XmlElement(required = true) 
    protected List<Constant> constant; 

    public List<Constant> getConstant() { 

但返回类型清单<> getConstant()方法不正确。我需要这是

public List<com.a.f.model.common.Constant> getConstant() { 

是否有通过jaxb绑定文件做到这一点?

回答

2

我工作围绕这通过使用Java泛型,使现有的接口在他们的返回类型

package com.a.f.m.common; 

import java.util.List; 

public interface Constants { 

    public List<? extends Constant> getConstant(); 
} 

更灵活由于JAXB生成的类常量确实实现了现有的接口常数,对方法的返回类型为允许。使用JAXB绑定文件声明返回类型似乎是不可能的。

相关问题