2012-08-03 77 views
2

我試圖映射使用莫西的XML元數據擴展以下界面。但是當我嘗試加載它時,我得到了下面的錯誤。因爲它是一個枚舉,所以我無法將一個公共構造函數添加到AddressTypeJAXB(莫西)XML元數據映射問題

我的問題是:爲什麼莫西IMPL看着AddressType即使我沒有在XML元數據規定?

public interface TokenizedUnitedStatesAddress 
{ 
    class AddressType extends Enum 
    { 
     public static final AddressType STREET = new AddressType("street");  
     public static final AddressType PO_BOX = new AddressType("poBox");  
     public static final AddressType RURAL_ROUTE = new AddressType("ruralRoute"); 

     public static AddressType getEnum(final String type) 
     { 
      return (AddressType) getEnum(AddressType.class, type); 
     } 

     protected AddressType(final String name) 
     { 
      super(name); 
     } 
    } 

    String getApartmentNumber(); 

    //removed other getters for brevity 
} 

<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/oxm http://www.eclipse.org/eclipselink/xsds/eclipselink_oxm_2_4.xsd" 
    version="2.4" package-name="com.abc.ic.domain.country.us"> 
    <java-types> 
     <java-type name="TokenizedUnitedStatesAddress"> 
     <xml-root-element /> 
     <xml-type 
      prop-order="StreetPreDirection StreetNumber StreetName StreetType StreetPostDirection UnitDesignator UnitNumber AddressLine1 AddressLine2 City State PostalCode CarrierRoute LengthAtAddress OwnershipStatus" /> 
     <java-attributes> 
      <xml-element name="StreetPreDirection" java-attribute="preDirectional" /> 
      <xml-element name="StreetNumber" java-attribute="houseNumber" /> 
      <xml-element name="StreetName" java-attribute="streetName" /> 
      <xml-element name="StreetType" java-attribute="streetType" /> 
      <xml-element name="StreetPostDirection" java-attribute="postDirection" /> 
      <xml-element name="UnitNumber" java-attribute="apartmentNumber" /> 
      <xml-element name="AddressLine1" java-attribute="primaryAddress" /> 
      <xml-element name="AddressLine2" java-attribute="secondaryAddress" /> 
      <xml-element name="City" java-attribute="cityName" /> 
      <xml-element name="State" java-attribute="stateAbbreviation" /> 
      <xml-element name="PostalCode" java-attribute="zipCode" /> 
     </java-attributes> 
     </java-type> 
    </java-types> 
</xml-bindings> 

javax.xml.bind.JAXBException: 
Exception Description: The class com.abc.ic.domain.country.us.TokenizedUnitedStatesAddress$AddressType requires a zero argument constructor or a specified factory method. Note that non-static inner classes do not have zero argument constructors and are not supported. 
- with linked exception: 
[Exception [EclipseLink-50001] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.JAXBException 
Exception Description: The class com.abc.ic.domain.country.us.TokenizedUnitedStatesAddress$AddressType requires a zero argument constructor or a specified factory method. Note that non-static inner classes do not have zero argument constructors and are not supported.] 
    at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:908) 
    at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:157) 
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:170) 
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:157) 
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:117) 
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:107) 
+0

這可能是一個錯誤,你可以在以下位置輸入一個:https://bugs.eclipse.org/bugs/enter_bug.cgi?product=EclipseLink。 – 2012-08-03 19:36:45

+0

「即使使用XML提供元數據,Mxoy Impl也會反思這個類」 - 這是否是正確的錯誤摘要?另外,如果你能解釋這個問題的答案,這將是一件好事。 – 2012-08-03 19:44:09

+0

另外,我可以修復這個bug你,如果你能提供我應該看看 – 2012-08-03 19:45:47

回答

0

我上的EclipseLink莫西團隊的開發人員,我一直在關注這個問題。你是正確的,爲什麼AddressType類是introspected,我看到你有一個解決方法。

另一個解決方案是創建一個可以在Apache枚舉類和它們的XML(串)表示之間轉換的XmlAdapter,具體如下:

import javax.xml.bind.annotation.adapters.XmlAdapter; 

import org.apache.commons.lang.enums.Enum; 

import enumbindings.TokenizedUnitedStatesAddress.AddressType; 

public class ApacheEnumAdapter extends XmlAdapter<String, Enum> { 

    public ApacheEnumAdapter() { 
    } 

    @Override 
    public Enum unmarshal(String s) throws Exception { 
     return AddressType.getEnum(s); 
    } 

    @Override 
    public String marshal(Enum e) throws Exception { 
     if (null == e) { 
      return null; 
     } 
     return e.getName(); 
    } 

} 

並鉤住了適配器的綁定文件是這樣的:

... 
<xml-element name="StreetType" java-attribute="streetType"> 
    <xml-java-type-adapter value="enumbindings.ApacheEnumAdapter" /> 
</xml-element> 
... 

至於EclipseLink的實際上是在這種情況下,行爲正確輸入的錯誤,我們不這樣做阿帕奇百科全書類的任何特殊處理,因此一個默認的無參數的構造函數(或其他處理機制)仍然是必需的。不過,我會更新您的bug並將其更改爲支持Apache Enums的增強請求,我們將對其進行評估。

感謝,

裏克

+0

Thx的信息。你還可以看看這個http://stackoverflow.com/questions/11849027/moxy-is-marshalling-unmapped-java-properties,讓我知道如果我需要爲此創建一個錯誤。 – 2012-08-07 19:49:06

1

MxOy型IMPL還是內省即使XML用於提供元數據的類。這是因爲,通過設計,外部映射文件用於擴充由註釋提供的元數據。然而

的問題是,公共土地枚舉抽象要求我們要有爲枚舉非公開單參數的構造函數。我通過添加初始化默認枚舉的公共無參數構造函數來解決此問題。這對我的應用程序已經足夠了。然而,我創建了一個可以遵循的錯誤here

注意:我也看了foctory-method option of Moxy,但它需要一個空arg方法作爲工廠方法,在Enum的情況下不是這種情況。