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 
Prats
  • 1,515
  • 6
  • 16
  • 30
  • Plz help me, I am stuck at it. Had this MOXy thing worked for anyone ? – Prats Jul 08 '13 at 13:08
  • Which application server are you using? – bdoughan Jul 09 '13 at 02:07
  • @BlaiseDoughan I am using Tomcat7 server. I was able to include MOXy's in my application, but it is not giving me the correct output. I have followed your both the links for implementing XmlInverseReference annotation: 1 (http://blog.bdoughan.com/2013/03/moxys-xmlinversereference-is-now-truly.html), 2) (http://stackoverflow.com/questions/3313454/npe-thrown-marshalling-entity-in-jax-rs). But, still I am getting wrong output. I have added the xml response in the question. Please have a look – Prats Jul 09 '13 at 07:20
  • @BlaiseDoughan In the above two links you have followed two different ways for implementing XmlInverseReference element. Could you please tell me which one is the correct way ? – Prats Jul 09 '13 at 07:24
  • Both ways are correct, it all depends if you want the mapping to be writable in both directions. I have added an answer to address the incorrect output: I have added an answer to correct the output: http://stackoverflow.com/a/17545727/383861. For new issues opening new questions makes things easier than adding to an existing question. – bdoughan Jul 09 '13 at 10:23
  • Oops.. will do it from now onwards – Prats Jul 09 '13 at 11:00

2 Answers2

3

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

Rick Barkhouse
  • 1,186
  • 2
  • 10
  • 15
2

To address your followup question where you are getting the wrong output. The mapping for the environmentproperty 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.

Prats
  • 1,515
  • 6
  • 16
  • 30
bdoughan
  • 147,609
  • 23
  • 300
  • 400