문제

I'm trying to use JAXB/MOXy to map a domain represented by a set of interfaces with a distinctive characteristic: all multivalued elements are encapsulated with Iterables as opposed to arrays or Collections. Below is a toy example to demonstrate my case.

Actor.java (note the use of Iterable<Movie>)

package test.moxy;

public interface Actor {

    public String getName();
    public void setName(String name);

    public Iterable<Movie> getMovies();
    public void setMovies(Iterable<Movie> movies);

}

Movie.java

package test.moxy;

public interface Movie {

    public String getTitle();
    public void setTitle(String title);

}

Main class

package test.moxy;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.xml.sax.InputSource;

public class TestBinding {

    public static void main(String[] args) throws FileNotFoundException, JAXBException {

        Class<?>[] types = new Class<?>[] { Actor.class, Movie.class };

        List<String> mappings = new ArrayList<String>();
        mappings.add("test/moxy/oxm.xml");

        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, mappings);

        JAXBContext jc = JAXBContext.newInstance(types, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        InputSource is = new InputSource(new FileInputStream("src/main/java/test/moxy/input.xml"));
        Actor actor = (Actor) unmarshaller.unmarshal(is);

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

    }

}

input.xml

<?xml version="1.0" encoding="UTF-8"?>
<actor>
    <name>John Smith</name>
    <movies>
        <movie>
            <title>Smith's Trilogy - Part I</title>
        </movie>
        <movie>
            <title>Smith's Trilogy - Part II</title>
        </movie>
        <movie>
            <title>Smith's Trilogy - Part III</title>
        </movie>
    </movies>
</actor>

oxm.xml

<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="test.moxy" xml-mapping-metadata-complete="true">
    <java-types>
        <java-type name="Actor">
        <xml-root-element name="actor" />
            <xml-type prop-order="name movies" factory-class="test.moxy.ProxyFactory"
                factory-method="initActor" />
            <java-attributes>
                <xml-element java-attribute="name" />
                <xml-element java-attribute="movies" name="movie" >
                    <xml-element-wrapper name="movies" />
                </xml-element>
            </java-attributes>
        </java-type>
        <java-type name="Movie">
            <xml-root-element name="movie" />
            <xml-type factory-class="test.moxy.ProxyFactory"
                factory-method="initMovie" />
            <java-attributes>
                <xml-element java-attribute="title" />
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

ProxyFactory is a standard factory such as the one exemplified in http://blog.bdoughan.com/2010/07/moxy-jaxb-map-interfaces-to-xml.html

As it stands, I get the following JAXBException: Exception Description: XmlElementWrapper is only allowed on a collection or array property but [movies] is not a collection or array property.

I tried with an XMLAdapter on movies to convert between Iterable<T> and List<T>, but it seems to apply to every movie element, as opposed to the movies wrapper. I was wondering if and how I could specify such a thing as some sort of XMLAdapter for wrappers?

도움이 되었습니까?

해결책

As the exception states you're not going to be able to map a property of type Iterable with @XmlElementWrapper.

Exception in thread "main" javax.xml.bind.JAXBException: 
Exception Description: XmlElementWrapper is only allowed on a collection or array property but [movies] is not a collection or array property.
 - with linked exception:
[Exception [EclipseLink-50015] (Eclipse Persistence Services - @VERSION@.@QUALIFIER@): org.eclipse.persistence.exceptions.JAXBException
Exception Description: XmlElementWrapper is only allowed on a collection or array property but [movies] is not a collection or array property.]
    at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1068)
    at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:182)

XmlAdapter (MoviesAdapter)

Instead we are going to use an XmlAdapter to convert Iterable into something we can map. In this case an instance of a class we'll make up called Movies that has a List of Movie instances.

package test.moxy;

import java.util.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class MoviesAdapter extends XmlAdapter<MoviesAdapter.Movies, Iterable<Movie>> {

    public static class Movies {
        public List<Movie> movie = new ArrayList<Movie>();
    }

    @Override
    public Iterable<Movie> unmarshal(Movies v) throws Exception {
        return v.movie;
    }

    @Override
    public Movies marshal(Iterable<Movie> v) throws Exception {
        Movies movies = new Movies();
        for(Movie movie : v) {
            movies.movie.add(movie);
        }
        return movies;
    }

}

External Metadata (oxm.xml)

Below is the modified oxm.xml that references the XmlAdapter. With the new mappings the Movies object will cause the movies element to come into existence so we won't need the @XmlElementWrapper metadata anymore.

<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="test.moxy" xml-mapping-metadata-complete="true">
    <java-types>
        <java-type name="Actor">
        <xml-root-element name="actor" />
            <xml-type prop-order="name movies" factory-class="test.moxy.ProxyFactory"
                factory-method="initActor" />
            <java-attributes>
                <xml-element java-attribute="name" />
                <xml-element java-attribute="movies">
                    <xml-java-type-adapter value="test.moxy.MoviesAdapter"/>
                </xml-element>
            </java-attributes>
        </java-type>
        <java-type name="Movie">
            <xml-root-element name="movie" />
            <xml-type factory-class="test.moxy.ProxyFactory"
                factory-method="initMovie" />
            <java-attributes>
                <xml-element java-attribute="title" />
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top