【问题标题】:JAXB Binding - defined the return type of List<T> methodsJAXB 绑定 - 定义 List<T> 方法的返回类型
【发布时间】:2011-07-13 23:48:30
【问题描述】:

我有这个模式并且我正在使用 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'

我还在包“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 jaxb jaxb2 maven-jaxb2-plugin


    【解决方案1】:

    我通过使用 java 泛型来解决这个问题,使现有接口的返回类型更加灵活

    package com.a.f.m.common;
    
    import java.util.List;
    
    public interface Constants {
    
        public List<? extends Constant> getConstant();
    }
    

    由于 JAXB 生成的类 Constant 确实实现了现有接口 Constant,因此方法上的返回类型是允许的。似乎无法使用 JAXB 绑定文件来声明返回类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 1970-01-01
      • 2011-05-09
      相关资源
      最近更新 更多