我有这个模式,我使用JAXB生成java存根文件。
<?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.a.f.models.types.common'
我也有'Constants'和'Constant'的现有接口,定义在包'com.a.f.model.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() {
但是List<> getConstant()方法的返回类型不正确。我需要这个是
public List<com.a.f.model.common.Constant> getConstant() {
是否有办法通过jaxb绑定文件做到这一点?
我通过使用java泛型使现有接口的返回类型更灵活来解决这个问题
package com.a.f.m.common;
import java.util.List;
public interface Constants {
public List<? extends Constant> getConstant();
}
由于JAXB生成的类Constant确实实现了现有的接口Constant,因此方法上的返回类型是允许的。似乎不可能使用JAXB绑定文件来声明返回类型。