Castor - MappingException Java类不可构造

时间:2015-05-12 17:20:27

标签: java castor

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

1 个答案:

答案 0 :(得分:0)

已解决:我得到异常的原因是因为我需要让我的内部类Static for Castor能够使用它。

网上其他地方有不少帖子建议使用

verify-constructable="false"

但内部类缺少静态是一个简单的错误。希望它可以帮助别人!

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