2015-08-13 29 views
1

文本在下面的代碼的Java類型,我發現了以下情況例外。 XmlAttribute/XmlValue不能正常工作,我是不是能夠識別: -獲取@ XmlAttribute/@ XmlValue需要引用映射在XML

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 

我的豆的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; 
    } 
} 

我的豆AddressComponent: -

@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 test.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> 

請推薦我所需的配置。

回答

2

在AddressComponent你錯誤類型設置爲一個屬性@XmlAttribute(名稱=「類型」)。這應該是一個xml元素。

+0

我已肯定它的作品嚐試過,但它給了我AddressComponent [LONGNAME = BMC殖民地,SHORTNAME = BMC殖民地,locationTypeSet =的locationType [locationTypeId = NULL,類型=空,狀態=空]的locationType [locationTypeId = NULL, type = null,status = null]]]我需要類型值是鄰域和政治而不是null。 –