目前 MOXy 不允许您将单个实例的表示拆分为 JSON(或 XML)树中的多个位置。请输入您希望查看的支持类型的增强请求:
下面我将演示如何映射部分 JSON 消息。
部分答案#1
下面我将描述如何将您的模型映射到以下 JSON 消息:
{
"meta" : {
"count" : 2
},
"people" : [ {
"id" : 1,
"name" : "Danny Parker",
"contact" : {
"locality" : "Zoo York",
"state" : "New York"
}
}, {
"id" : 2,
"name" : "Oscar the Grouch",
"contact" : {
"locality" : "San Francisco",
"state" : "California"
}
} ]
}
oxm.xml
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum11870107">
<java-types>
<java-type name="PersonCollectionResponse">
<java-attributes>
<xml-element java-attribute="count" xml-path="meta/count/text()"/>
</java-attributes>
</java-type>
<java-type name="Person">
<xml-type prop-order="id name address"/>
<java-attributes>
<xml-element java-attribute="address" name="contact"/>
</java-attributes>
</java-type>
<java-type name="Address">
<java-attributes>
<xml-transient java-attribute="zip"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
部分答案#2
现在我将描述如何将您的模型映射到以下 JSON 消息:
{
"meta" : {
"count" : 2
},
"mappings" : [ {
"person" : 2,
"zip" : "94102"
}, {
"person" : 1,
"zip" : "10014"
} ]
}
oxm.xml
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum11870107">
<java-types>
<java-type name="PersonCollectionResponse">
<java-attributes>
<xml-element java-attribute="count" xml-path="meta/count/text()"/>
<xml-element java-attribute="people" name="mappings"/>
</java-attributes>
</java-type>
<java-type name="Person">
<xml-type prop-order="id address"/>
<java-attributes>
<xml-element java-attribute="id" name="person"/>
<xml-transient java-attribute="name"/>
<xml-element java-attribute="address" xml-path="."/>
</java-attributes>
</java-type>
<java-type name="Address" xml-accessor-type="NONE">
<java-attributes>
<xml-element java-attribute="zip"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
演示代码
两个部分解决方案都可以使用相同的演示代码:
package forum11870107;
import java.util.*;
import javax.xml.bind.*;
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, "forum11870107/oxm.xml");
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
JAXBContext jc = JAXBContext.newInstance(new Class[] {PersonCollectionResponse.class}, properties);
PersonCollectionResponse pcr = new PersonCollectionResponse();
Person person1 = new Person();
person1.setId(1);
person1.setName("Danny Parker");
pcr.getPeople().add(person1);
Address address1 = new Address();
address1.setLocality("Zoo York");
address1.setState("New York");
address1.setZip("10014");
person1.setAddress(address1);
Person person2 = new Person();
person2.setId(2);
person2.setName("Oscar the Grouch");
pcr.getPeople().add(person2);
Address address2 = new Address();
address2.setLocality("San Francisco");
address2.setState("California");
address2.setZip("94102");
person2.setAddress(address2);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(pcr, System.out);
}
}
领域模型
以下是用于两个部分解决方案的域模型:
PersonCollectionResponse
package forum11870107;
import java.util.*;
public class PersonCollectionResponse {
private Set<Person> people = new HashSet<Person>();
public Set<Person> getPeople() {
return this.people;
}
public void setPeople(Set<Person> people) {
this.people = people;
}
public int getCount() {
return this.people.size();
}
}
人
package forum11870107;
public class Person {
private Integer id;
private String name;
private Address address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
地址
package forum11870107;
public class Address {
private String street;
private String locality;
private String state;
private String zip;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getLocality() {
return locality;
}
public void setLocality(String locality) {
this.locality = locality;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
}
jaxb.properties
要将 MOXy 指定为您的 JAXB 提供程序,您需要包含一个jaxb.properties
在与域模型相同的包中调用的文件,其中包含以下条目(请参阅:http ://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as -你的.html。
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory