有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

获取@XmlAttribute/@XmlValue需要引用映射到XML中文本的Java类型

在下面的代码中,我得到了以下异常。XmlAttribute/XmlValue无法正常工作,我无法识别:-

    com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
@XmlAttribute/@XmlValue need to reference a Java type that maps to text in XML.
    this problem is related to the following location:
        at public java.util.Set nl.magnus.test.AddressComponent.getLocationTypeSet()
        at nl.magnus.test.AddressComponent

My bean LocationType:-

@XmlRootElement(name = "type")
public class LocationType {

    private Integer locationTypeId;
    private String type;
    private String status;
    @Override
    public String toString() {
        return "LocationType [locationTypeId=" + locationTypeId + ", type=" + type + ", status=" + status + "]";
    }
    public Integer getLocationTypeId() {
        return locationTypeId;
    }
    public void setLocationTypeId(Integer locationTypeId) {
        this.locationTypeId = locationTypeId;
    }
    public String getType() {
        return type;
    }
    @XmlAttribute(name = "type")
    public void setType(String type) {
        this.type = type;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
}

我的bean地址组件:-

    @XmlRootElement(name = "address_component")
public class AddressComponent {

    private String longName;
    private String shortName;
    private Set<LocationType> locationTypeSet;

    public String getLongName() {
        return longName;
    }
    @XmlElement(name = "long_name")
    public void setLongName(String longName) {
        this.longName = longName;
    }
    public String getShortName() {
        return shortName;
    }
    @XmlElement(name = "short_name")
    public void setShortName(String shortName) {
        this.shortName = shortName;
    }
    public Set<LocationType> getLocationTypeSet() {
        return locationTypeSet;
    }
    @XmlAttribute(name = "type")
    public void setLocationTypeSet(Set<LocationType> locationTypeSet) {
        this.locationTypeSet = locationTypeSet;
    }

    @Override
    public String toString() {
        return "AddressComponent [longName=" + longName + ", shortName=" + shortName + ", locationTypeSet="
                + locationTypeSet + "]";
    }
}

我的测试类TestSmall:-

public class TestSmall {
    public static void main(String[] args) {

        try {

            File file = new File("test.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(AddressComponent.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            AddressComponent geoResponse = (AddressComponent) jaxbUnmarshaller.unmarshal(file);
            System.out.println(geoResponse);

//          Location latLong = geoResponse.getaResult().getGeometry().getLocation();
//          System.out.println(latLong.getLatitude()+ ", "+latLong.getLongitude());

        } catch (JAXBException e) {
            e.printStackTrace();
        }

    }
}

我的xml测试。xml:-

    <?xml version="1.0" encoding="UTF-8"?>
<address_component>
   <long_name>BMC Colony</long_name>
   <short_name>BMC Colony</short_name>
   <type>neighborhood</type>
   <type>political</type>
</address_component>

请建议我所需的配置


共 (1) 个答案

  1. # 1 楼答案

    在AddressComponent中,您错误地将type设置为属性@xmltribute(name=“type”)。这应该是一个xml元素