Eclipse Moxy在某些情况下会忽略@XmlNamedObjectGraph

时间:2018-11-15 15:48:43

标签: java moxy xml-binding

我有很多使用eclipse MOXy @XmlNamedObjectGraphs转换为XML的类。除了一个类-TaskSchedule:

@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlNamedObjectGraphs({
  @XmlNamedObjectGraph(
          name="full",
          attributeNodes = {
                  @XmlNamedAttributeNode( "businessEntityNumber" ),
                  @XmlNamedAttributeNode( "businessEntityClass" ),
                  @XmlNamedAttributeNode( value="task", subgraph="simple" ),
                  @XmlNamedAttributeNode( "cron" ),
                  @XmlNamedAttributeNode( "enabled" ),
                  @XmlNamedAttributeNode( "endDate" ),
                  @XmlNamedAttributeNode( "name" ),
                  @XmlNamedAttributeNode( "period" ),
                  @XmlNamedAttributeNode( value="region", subgraph="simple" ),
                  @XmlNamedAttributeNode( value="productGroup", subgraph="simple" ),
                  @XmlNamedAttributeNode( value="user", subgraph="simple" ),
                  @XmlNamedAttributeNode( "scheduleType" ),
                  @XmlNamedAttributeNode( "startDate" ),
                  @XmlNamedAttributeNode( value="parameterGroup", subgraph="full" )
          }),
  @XmlNamedObjectGraph(
          name="simple",
          attributeNodes = {
                  @XmlNamedAttributeNode( "businessEntityNumber" ),
                  @XmlNamedAttributeNode( "businessEntityClass" ),
                  @XmlNamedAttributeNode( "name" )
          }),
  @XmlNamedObjectGraph(
          name="child_group", // used when as a child of a group - some fields dont apply
          attributeNodes = {
                  @XmlNamedAttributeNode( "businessEntityNumber" ),
                  @XmlNamedAttributeNode( "businessEntityClass" ),
                  @XmlNamedAttributeNode( "name" ),
                  @XmlNamedAttributeNode( value="region", subgraph="simple" ),
                  @XmlNamedAttributeNode( value="productGroup", subgraph="simple" ),
                  @XmlNamedAttributeNode( value="task", subgraph="simple" ),
                  @XmlNamedAttributeNode( value="parameterGroup", subgraph="full" )
          })
})
public class TaskSchedule extends AbstractBusinessEntity {
  protected Task task;
  protected String cron;
  protected Boolean enabled;
  protected Date endDate;
  protected String name;
  protected ExecutionPeriod period;
  protected Region region;
  protected ProductGroup productGroup;
  protected TaskScheduleType scheduleType;
  protected Date startDate;
  protected TaskParameterGroup parameterGroup;
  protected User user;

  // get/set methods
}

将日程表转换为XML时,如果该日程表是要转换的顶级元素(即使用“完整”图),则一切正常。但是我还有其他类-TaskGroup和TaskGroupSchedule:

@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlNamedObjectGraphs({
  @XmlNamedObjectGraph(
          name="full",
          attributeNodes = {
                  @XmlNamedAttributeNode( "businessEntityNumber" ),
                  @XmlNamedAttributeNode( "businessEntityClass" ),
                  @XmlNamedAttributeNode( "name" ),
                  @XmlNamedAttributeNode( value="createdBy", subgraph="simple" ),
                  @XmlNamedAttributeNode( "createdOn" ),
                  @XmlNamedAttributeNode( value="schedules", subgraph="simple" ),
          }
          ),
  @XmlNamedObjectGraph(
          name="simple",
          attributeNodes = {
                  @XmlNamedAttributeNode( "businessEntityNumber" ),
                  @XmlNamedAttributeNode( "businessEntityClass" ),
                  @XmlNamedAttributeNode( "name" )
          }
          )
})
public class TaskGroup extends AbstractBusinessEntity {
  protected String name;
  protected User createdBy;
  protected Date createdOn; 
  protected Set<TaskGroupSchedule> schedules;

  // get/set methods
}

TaskGroupSchedule:

@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlNamedObjectGraphs({
  @XmlNamedObjectGraph(
          name="child",
          attributeNodes = {
                  @XmlNamedAttributeNode( "id" ),
                  @XmlNamedAttributeNode( "className" ),
                  @XmlNamedAttributeNode( value="schedule", subgraph="child_group" ),
                  @XmlNamedAttributeNode( "sequence" ),
                  @XmlNamedAttributeNode( "enabled" ),
                  @XmlNamedAttributeNode( "stopOnError" ),
                  @XmlNamedAttributeNode( "delayBeforeNext" ),
          })
})
public class TaskGroupSchedule extends AbstractManagedData implements IEnableable, IRegional, Sequenceable {

  protected TaskGroup parent;
  protected TaskSchedule schedule;
  protected int sequence;
  protected Boolean enabled;
  protected long delayBeforeNext;

  // get/set methods, with @XMLTransient on getParent
}

出现问题:当我尝试将TaskGroup转换为XML时,它忽略了我对TaskGroupSchedule-> TaskSchedule使用'child_group'子图的请求,而是通过TaskSchedule的每个属性进行转换(导致意外的“ 在对象图中检测到一个循环,这会对引用返回父级的项造成无限深的XML 错误,我猜这是默认行为,如果没有定义图。我正在使用MOXy版本2.6.3和Java 8。

有人可以看到我在做什么吗?预先感谢。

1 个答案:

答案 0 :(得分:0)

好的,我错过了一个明显的问题。

这是因为TaskGroup图假定TaskGroupSchedule具有定义的'simple'图而不是'child'图。在TaskGroup中将'simple'更改为'child'使其起作用。