0

我有很多类使用 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
}

任务组计划:

@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。

谁能看到我做错了什么?提前致谢。

4

1 回答 1

0

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

这是因为该TaskGroup图假设定义TaskGroupSchedule了一个'simple'图而不是'child'图。在 TaskGroup 中更改'simple''child'使其工作。

于 2018-11-20T16:28:29.893 回答