Reputation: 1525
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:
Downloaded EclipseLink from [http://www.eclipse.org/eclipselink/downloads/nightly.php]
Copied all the JARs from /eclipselink/jlib folder to my /WEB-INF/lib
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
Upvotes: 2
Views: 480
Reputation: 149017
To address your followup question where you are getting the wrong output. The mapping for the environment
property on your ApplicationDTO
is wrong. You could do one of the following:
Option #1 - @XmlElementRef
If you use the @XmlElementRef
annotation the element name for this property will be based on the @XmlRootElement
annotation on the target class.
@OneToMany(mappedBy = "application", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@XmlElementRef
public Set<EnvironmentDTO> getEnvironment() {
return environment;
}
Option #2 - @XmlElement
Alternatively you can use the @XmlElement
annotation to specify the element that should be used for the property.
@OneToMany(mappedBy = "application", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@XmlElement(name="environmentDTO")
public Set<EnvironmentDTO> getEnvironment() {
return environment;
}
EDIT:
@GET
@Path("/get")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public List<ApplicationDTO> getAllApplications(){
List<ApplicationDTO> allApplication = applicationService.getAllApplication();
return allApplication;
}
From here flow comes to the following class:
@Override
public List<ApplicationDTO> getAllApplication() {
List<ApplicationDTO> AppList = genericDAOTxService.findAll(ApplicationDTO.class);
return AppList;
}
Than to the following class:
@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;
}
And, then from database values are getting populated.
Previously i.e. before using MOXy's I used to get following in ApplicationDTO object:
[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]]]]
But, now I am getting following in the ApplicationDTO object:
[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]]
This is the actual flow of my application and object is getting created automatically using JAXB and Jersy libraries. I am not having any other seperate program for marshalling and unmarshalling the object.
Upvotes: 2
Reputation: 1206
For some reason, your jaxb.properties
file was not picked up -- the "com.sun.xml.bind.v2.runtime" in the error message indicates that Sun JAXB is running, not MOXy.
Make sure that jaxb.properties
is on the classpath in the same package as your model classes. If you included jaxb.properties
in your source directory, ensure that it was copied to the same directory where your .class
files end up. You can read about setting up your jaxb.properties
here: http://www.eclipse.org/eclipselink/documentation/2.5/moxy/runtime.htm#sthref8
Hope this helps, Rick
Upvotes: 3