1

I have an XML which needs to be mapped to a Java object i.e. a DTO. My XML has some wrapper elements which don't have any java objects in my DTO.. my XML looks something like this

<UseCaseView>
<FindCandidates>
    <CandidatesRequest>
        <APIRequest>
            <Code>Code</Code>
        </APIRequest>
    </CandidatesRequest>
</FindCandidates>   </UseCaseView>

The "FindCandidates" and "CandidatesRequest" are just wrapper elements and "APIRequest" is again a DTO..

I am using XMLPath like this in my DTO.. My Dto looks like this..

@XmlRootElement(name = "UseCaseView")
 public class FindRequestDTO implements Serializable{

private static final long serialVersionUID = 5528726225975606325L;

private ApiRequestDTO apiRequest;


@XmlPath("FindCandidates/CandidatesRequest/APIRequest")
public ApiRequestDTO getAPIRequest() {
    return apiRequest;
    .........

This is not mapping the APIRequest element to my ApiRequestDTO, if I remove the two wrapper elements and map directly using XMLElement(name = "APIRequest") it works... But I need to ignore the two wrapper elements and construct my DTO.. I have added the Jaxb.properties file with

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

in my resources folder.

can someone help me to know whats going wrong here..

thanks,

bdoughan
  • 147,609
  • 23
  • 300
  • 400
Sampath Pasupunuri
  • 628
  • 1
  • 13
  • 25
  • When you create the `JAXBContext` what is the implementation class that is returned? The following example may help:https://github.com/bdoughan/blog20110908. If not I'll post an answer tomorrow. – bdoughan Mar 26 '12 at 23:27
  • @Blaise - thx for quick reply.. the implementation class returned is com.sun.xml.bind.v2.runtime.JAXBContextImpl – Sampath Pasupunuri Mar 26 '12 at 23:36
  • Do you have the eclipselink.jar on your classpath? The example I linked to in my previous comment is all setup to compile and run using Maven. – bdoughan Mar 26 '12 at 23:42
  • Yeah.. my setup looks same.. I have the dependency added and have eclipselink.jar in my classpath... I am doing it in the same way as u did in your googledemo.. But in YahooDemo, you have some binding.xml.. I am not doing that way... – Sampath Pasupunuri Mar 27 '12 at 00:00
  • 1
    Do you have the jaxb.properties in the same package as your domain model? – bdoughan Mar 27 '12 at 00:08
  • appreciate your patience dude.. this is how I am creating the context JAXBContext context = JAXBContext.newInstance(toClass); – Sampath Pasupunuri Mar 27 '12 at 00:23
  • what should be the implementation class that it should return? – Sampath Pasupunuri Mar 27 '12 at 00:23
  • 1
    You should get back org.eclipse.persistence.jaxb.JAXBContext. Where you able to run the example I had on GitHub? – bdoughan Mar 27 '12 at 00:32
  • 1
    You could avoid the normal bootstrap process and use one of the org.eclipse.persistence.jaxb.JAXBContextFactory.createContext methods. – bdoughan Mar 27 '12 at 00:40
  • 1
    Thanks a lot buddy, I got that thought but I was wondering what to pass in for properties, I just passed null and it worked out.. I really appreciate your help.. thanks... – Sampath Pasupunuri Mar 27 '12 at 00:44

1 Answers1

2

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.

Below is a complete example that should help.

jaxb.properties

To specify MOXy as your JAXB provider you need to add a file called jaxb.properties with the following entry in the same package as your domain model.

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

FindRequestDTO

package forum9881188;

import java.io.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.*;

@XmlRootElement(name = "UseCaseView")
public class FindRequestDTO implements Serializable {

    private static final long serialVersionUID = 5528726225975606325L;

    private ApiRequestDTO apiRequest;

    @XmlPath("FindCandidates/CandidatesRequest/APIRequest")
    public ApiRequestDTO getAPIRequest() {
        return apiRequest;
    }

    public void setAPIRequest(ApiRequestDTO apiRequest) {
        this.apiRequest = apiRequest;
    }

}

ApiRequestDTO

package forum9881188;

public class ApiRequestDTO {
}

Demo

package forum9881188;

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(FindRequestDTO.class);

        FindRequestDTO fr = new FindRequestDTO();
        fr.setAPIRequest(new ApiRequestDTO());

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(fr, System.out);
    }

}

Output

<?xml version="1.0" encoding="UTF-8"?>
<UseCaseView>
   <FindCandidates>
      <CandidatesRequest>
         <APIRequest/>
      </CandidatesRequest>
   </FindCandidates>
</UseCaseView>

For More Information


UPDATE

If for some reason you can't get the MOXy implementation of JAXBContext, you can always use the native APIs to bootstrap. When using the native APIs you do not need the jaxb.properties file:

package forum9881188;

import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        //JAXBContext jc = JAXBContext.newInstance(FindRequestDTO.class);
        JAXBContext jc = JAXBContextFactory.createContext(new Class[] {FindRequestDTO.class}, null);

        FindRequestDTO fr = new FindRequestDTO();
        fr.setAPIRequest(new ApiRequestDTO());

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(fr, System.out);
    }

}
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • When I try the update solution I got :Caught: groovy.lang.MissingMethodException: No signature of method: static org.eclipse.persistence.jaxb.JAXBContextFactory.createContext() is applicable for argument types: ([Ljava.lang.Object;, null) values: [[class CommonSvcRs], null] Possible solutions: createContext([Ljava.lang.Class;, java.util.Map), createContext(java.lang.String, java.lang.ClassLoader), createContext([Ljava.lang.Class;, java.util.Map, java.lang.ClassLoader), createContext([Ljava.lang.reflect.Type;, java.util.Map, java.lang.ClassLoader), ... – Norm212 Apr 08 '14 at 20:12