注意:我是EclipseLink JAXB (MOXy)负责人,也是JAXB (JSR-222)专家组的成员。
MOXy 没有直接等同于 Jackson 的@JsonAnySetter
. 我已输入以下增强请求以添加此类行为:
以下是一些可能适用于您的用例的 MOXy 扩展的一些信息。
莫西的@XmlVirtualAccessMethods
如果 JSON 属性不是真的未知,它们只是不作为属性存在于您的域模型上,那么您可以使用 MOXy 的@XmlVirtualAccessMethods
扩展(参见: http ://blog.bdoughan.com/2011/06/extensible-models-with -eclipselink-jaxb.html )。
Java 模型
顾客
@XmlVirtualAccess
方法注解用于指定Customer
类是可扩展 的。
import java.util.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlVirtualAccessMethods;
@XmlVirtualAccessMethods(setMethod = "put")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
private String firstName;
private Address billingAddress;
@XmlTransient
private Map<String, Object> extensions = new HashMap<String, Object>();
public <T> T get(String property) {
return (T) extensions.get(property);
}
public void put(String property, Object value) {
extensions.put(property, value);
}
}
地址
package forum18068176;
public class Address {
private String street;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
映射文件 (oxm.xml)
扩展属性的定义在 MOXy 的映射文档中定义(参见: http ://blog.bdoughan.com/2011/04/moxys-xml-metadata-in-jax-rs-service.html )。
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum18068176">
<java-types>
<java-type name="Customer">
<xml-type prop-order="firstName lastName billingAddress shippingAddress"/>
<java-attributes>
<xml-element
java-attribute="lastName"
type="java.lang.String"/>
<xml-element
java-attribute="shippingAddress"
type="forum18068176.Address"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
演示
下面是一些独立的演示代码,您可以运行它来查看一切是如何工作的。您需要将 MOXy 指定为您的 JAXB 提供程序(请参阅: http ://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html )。
package forum18068176;
import java.util.*;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(1);
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum18068176/oxm.xml");
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
JAXBContext jc = JAXBContext.newInstance(new Class[] {Customer.class}, properties);
// Unmarshal JSON
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource json = new StreamSource("src/forum18068176/input.json");
Customer customer = unmarshaller.unmarshal(json, Customer.class).getValue();
// Access Extension Properties
String lastName = customer.<String>get("lastName");
Address shippingAddress = customer.<Address>get("shippingAddress");
// Marshal Objects
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(customer, System.out);
}
}
输入.json/输出
{
"firstName" : "Jane",
"lastName" : "Doe",
"billingAddress" : {
"street" : "1 A Street"
},
"shippingAddress" : {
"street" : "2 B Road"
}
}
MOXy 的 @XmlVariableNode 扩展,带有XmlAdapter
如果所有未知项目都属于同一类型,那么您可以使用 MOXy 的组合@XmlVariableNode
(参见: http ://blog.bdoughan.com/2013/06/moxys-xmlvariablenode-json-schema.html )并XmlAdapter
获取期望的结果: