开发者

JAXB Binding - defined the return type of List<T> methods

I have this schema and i'm using JAXB to generate java stub files.

<?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>

The default java package name is 'com.a.f.models.types.common'

I also have existing interfaces for 'Constants' and 'Constant' defined in package 'com.a.f.model.common' which i want the generated classes to use. I'm using the jaxb binding file to ensure the generated java classes implement the required interfaces

<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>

The generated class below does implement the correct interface

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() {

But the return type of the List<> getConstant() method is not correct. I need this to be

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

Is there away to do this via the jaxb binding file?


I worked around this by using java Generics to make the existing interfaces more flexible in their return type

package com.a.f.m.common;

import java.util.List;

public interface Constants {

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

Since the JAXB generated class Constant does implement the existing interface Constant, the return type on the method is allowed. It doesn't seem possible to use the JAXB bindings file to declare the return type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜