2

I was getting a cyclic error in my DTO classes, so decided to implement MOXy's to get rid of it. I followed the following steps for implementing MOXy's:

  1. Downloaded EclipseLink from [http://www.eclipse.org/eclipselink/downloads/nightly.php]

  2. Copied all the JARs from /eclipselink/jlib folder to my /WEB-INF/lib

  3. For Specifying EclipseLink MOXy as the JAXB provider created a jaxb.properties file in the folder where all classes are present with following entry in it -

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

I am using @XmlElement and @XmlInverseReference annotations in my dto classes but I am getting following error while I am running my application:

javax.ws.rs.WebApplicationException: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions, Class has two properties of the same name "application"
this problem is related to the following location:
    at public thbs.provisioning.dto.ApplicationDTO thbs.provisioning.dto.EnvironmentDTO.getApplication()
    at thbs.provisioning.dto.EnvironmentDTO
    at public java.util.Set thbs.provisioning.dto.ApplicationDTO.getEnvironment()
    at thbs.provisioning.dto.ApplicationDTO
this problem is related to the following location:
    at private thbs.provisioning.dto.ApplicationDTO thbs.provisioning.dto.EnvironmentDTO.application
    at thbs.provisioning.dto.EnvironmentDTO
    at public java.util.Set thbs.provisioning.dto.ApplicationDTO.getEnvironment()
    at thbs.provisioning.dto.ApplicationDTO 
4

2 回答 2

3

由于某种原因,您的jaxb.properties文件未被拾取——错误消息中的“com.sun.xml.bind.v2.runtime”表明 Sun JAXB 正在运行,而不是 MOXy。

确保它jaxb.properties位于与模型类相同的包中的类路径上。如果您包含jaxb.properties在源目录中,请确保将其复制到.class文件最终所在的同一目录中。您可以在此处阅读有关设置的信息:http jaxb.properties: //www.eclipse.org/eclipselink/documentation/2.5/moxy/runtime.htm#sthref8

希望这会有所帮助,瑞克

于 2013-07-08T15:03:53.033 回答
2

解决您在哪里得到错误输出的后续问题。environment您的属性的映射ApplicationDTO是错误的。您可以执行以下操作之一:

选项1 -@XmlElementRef

如果您使用@XmlElementRef注解,则此属性的元素名称将基于@XmlRootElement目标类上的注解。

@OneToMany(mappedBy = "application", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@XmlElementRef
public Set<EnvironmentDTO> getEnvironment() {
    return environment;
}

选项 #2 -@XmlElement

或者,您可以使用@XmlElement注释来指定应用于属性的元素。

@OneToMany(mappedBy = "application", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@XmlElement(name="environmentDTO")
public Set<EnvironmentDTO> getEnvironment() {
    return environment;
}

编辑:

@GET
@Path("/get")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public List<ApplicationDTO> getAllApplications(){
    List<ApplicationDTO> allApplication = applicationService.getAllApplication();
    return allApplication;
}

从这里流到以下类:

@Override
public List<ApplicationDTO> getAllApplication() {

    List<ApplicationDTO> AppList = genericDAOTxService.findAll(ApplicationDTO.class);   
    return AppList;     
}

比以下课程:

@Override
@Transactional(rollbackFor = java.lang.Exception.class, noRollbackFor = java.io.FileNotFoundException.class, propagation = Propagation.REQUIRES_NEW)
public <T> List<T> findAll(Class<T> type) {
    List<T> list = genericDAOImpl.findAll(type);
    return list;
}

然后,从数据库中填充值。

以前,即在使用 MOXy 之前,我曾经在 ApplicationDTO 对象中获得以下信息:

[ApplicationDTO [applicationId=1, applicationName=name, applicationDescription=desc, owner=5, createTime=2013-05-06 15:29:15.0, environment=[EnvironmentDTO [environmentId=1, environmentName=envname]]], ApplicationDTO [applicationId=2, applicationName=name, applicationDescription=desc, owner=5, createTime=2013-05-28 19:07:10.0, environment=[EnvironmentDTO [environmentId=2, environmentName=envname]]]]

但是,现在我在 ApplicationDTO 对象中得到关注:

[ApplicationDTO [applicationId=1, applicationName=name, applicationDescription=desc, owner=5, createTime=2013-05-06 15:29:15.0], ApplicationDTO [applicationId=2, applicationName=name, applicationDescription=desc, owner=5, createTime=2013-05-28 19:07:10.0]]

这是我的应用程序的实际流程,对象是使用 JAXB 和 Jersy 库自动创建的。我没有任何其他单独的程序来编组和解组对象。

于 2013-07-09T10:19:46.043 回答