使用Castor 1.3,在测试时收到以下异常:

org.exolab.castor.mapping.MappingException: The Java class com.company.sales.app.valueobjects.eai.ContactInformationValueObject$CommunicationPreference is not constructable -- it does not contain a default public constructor


尝试在我的Java代码中使用公共内部类的集合。

类的例子:

public class Car {
   public String color;
   public String make;
   public String model;
   public List<RoadTrip> roadTrips;

   public class RoadTrip {
      public String pointA;
      public String pointB;
   }
}


XML映射:

<class name="com.auto.Car" >
    <field name="color" type="java.lang.String"><bind-xml name="COLOR" /></field>
    <field name="make" type="java.lang.String"><bind-xml name="MAKE" /></field>
    <field name="model" type="java.lang.String"><bind-xml name="MODEL" /></field>
    <field name="roadTrips" type="com.auto.Car$RoadTrip"><bind-xml name="TRIP" /></field>
</class>
<class name="com.auto.Car$RoadTrip">
    <field name="pointA" type="java.lang.String"><bind-xml name="STARTING_POINT" /></field>
    <field name="pointB" type="java.lang.String"><bind-xml name="DESTINATION" /></field>
</class>


XML范例

<AUTOMOBILE>
    <COLOR>LITTLE RED</COLOR>
    <MAKE>CHEVROLET</MAKE>
    <MODEL>CORVETTE</MODEL>
    <TRIP>
        <STARTING_POINT>DALLAS</STARTING_POINT>
        <DESTINATION>VEGAS</DESTINATION>
    </TRIP>
    <TRIP>
        <STARTING_POINT>SEATTLE</STARTING_POINT>
        <DESTINATION>PORTLAND</DESTINATION>
    </TRIP>
</AUTOMOBILE>

最佳答案

解决:之所以得到例外是因为我需要使Castor的内部类为Static,以便能够使用它。

网络上其他地方有很多建议使用

verify-constructable="false"


但是在内部类上缺少static是一个简单的错误。希望它能帮助别人!

public class Car {
   public String color;
   public String make;
   public String model;
   public List<RoadTrip> roadTrips;

   public static class RoadTrip {
      public String pointA;
      public String pointB;
   }
}


http://castor.codehaus.org/1.2/how-to-map-an-inner-class.html

10-06 10:19