- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 JAXB/MOXy 来映射由一组具有独特特征的接口(interface)表示的域:所有多值元素都用 Iterables
封装。与数组或 Collection
相反s。下面是一个演示我的案例的玩具示例。
Actor.java(注意 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);
}
主类
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
是一个标准工厂,如 http://blog.bdoughan.com/2010/07/moxy-jaxb-map-interfaces-to-xml.html 中示例的工厂。
就目前情况而言,我得到以下内容 JAXBException
:异常描述:XmlElementWrapper
仅允许在集合或数组属性上使用,但 [movies] 不是集合或数组属性。
我尝试过 XMLAdapter
在电影 Iterable<T>
之间进行转换和List<T>
,但它似乎适用于每个 movie
元素,而不是 movies
wrapper 。我想知道是否以及如何将这样的东西指定为某种 XMLAdapter
用于包装?
最佳答案
由于异常表明您将无法使用 @XmlElementWrapper
映射 Iterable
类型的属性。
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(电影适配器)
相反,我们将使用 XmlAdapter
将 Iterable
转换为我们可以映射的内容。在本例中,我们将创建一个名为 Movies
的类实例,该实例具有 Movie
实例的 List
。
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;
}
}
外部元数据 (oxm.xml)
下面是引用 XmlAdapter
的修改后的 oxm.xml
。使用新映射,Movies
对象将导致 movies
元素出现,因此我们不再需要 @XmlElementWrapper
元数据。
<?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>
关于java - JAXB/MOXy 调整 XMLElementWrapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19542533/
我正在尝试创建一个类似于下面提到的 xml 文件。 value1 value2 能否请您告诉我,如何定义类层次结构。
在我的代码中我有这个 bean: @XmlRootElement @XmlAccessorType(XmlAccessType.NONE) public class Data implements S
我在编码具有双向关系的类时遇到问题。以下示例将通过编码 PhoneNumber 实例来生成无效的 xml。 yvonne 12345
文档说明 @XmlElementWrapper 注释可用于“未包装”或“包装”集合。 http://docs.oracle.com/javaee/5/api/javax/xml/bind/annota
下面有这段代码 @XmlRootElement(name = "FNOL") @XmlAccessorType(XmlAccessType.FIELD) public class Conversati
我正在使用 jaxb 创建以下 xml 结构: @XmlElementWrapper @XmlElement(name = "picture") private List pictures; 创建:
我有一个 XML 要使用 JAXB 加载。它加载正确,除了 getEffectSettings() 大小为 0,而它应该显示 2,因为我在“settings-list”元素中有两个“effect-se
我正在尝试使用 JAXB/MOXy 来映射由一组具有独特特征的接口(interface)表示的域:所有多值元素都用 Iterables 封装。与数组或 Collection 相反s。下面是一个演示我的
如何告诉 JAXB 对包装在 @XmlElementWrapper 集合中的数据使用 @XmlRootElement 注释?我知道您可以使用 @XmlElement 来命名每个单独的项目,但我很好奇是
我正在使用 Swagger 为基于 jax-rs 的 API 生成文档。在我的一个模型中,我具有以下属性: @XmlElementWrapper(name = "clip_list") @XmlEle
我想生成如下所示的 XML: 这就是我在代码中生成 mainNode1 、 mainNode2 和 node1 的方式: @Xml
我们使用 Jackson 2.9.4 和 JaxB Annotations 序列化为 JSON。 @XMLElementWrapper 注释没有按预期添加额外的包装级别。相反,它只是更改元素的名称。
我有一个如下所示的 XML 文档,并希望对其执行解码 Lon
我遇到了一个字段的 JAXB 注释问题,该字段是一个泛型类型为接口(interface)的列表。当我声明它时: @XmlAnyElement private List animals; 每件事都正常工
我有一个类,其中有一些我想通过 Jackson 进行编码的字符串列表。为了更好地使用,我想在每个列表中列出相同的元素名称。所以我这样注释: import java.util.List; import
我正在尝试根据 Jersey/Jaxb aliasing a List of beans 上接受的答案在 Artcile 的评论列表周围添加一个包装类 public class Article imp
我想为 xml 根元素和列表元素指定自定义名称。但注释不起作用。 @XmlRootElement(name = "test") @XmlAccessorType(XmlAccessType.FIELD
我正在使用 jackson-databind:2.9.3 和 jackson-module-jaxb-annotations:2.9.3 我有以下格式的 JSON。 { "response":
我有一个带有 XmlElementWrapper 注释的类,例如: ... @XmlElementWrapper(name="myList") @XmlElements({ @Xm
如何将属性添加到XmlElementWrapper?我觉得很难做到这一点。我找不到讨论它的文章或教程和问题。 我希望我的 XML 像 ff:
我是一名优秀的程序员,十分优秀!