我正在尝试使用MOXy为前端客户端生成特定格式的响应,以便他们更容易确定特定的关系。这是一个人为的例子,但我正在尝试做类似以下的事情......
public class Person {
private Integer id;
private String name;
private Address address;
}
public class Address {
private String street;
private String locality;
private String state;
private String zip;
}
public class PersonCollectionResponse {
private Set<Person> people;
public Set<Person> getPeople() {
return this.people;
}
private int getCount() {
return this.people.size();
}
}
...并希望得到像......这样的结果。
{
"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"
}
}],
"mappings": [{
"person": 1,
"zip": 10014
}, {
"person": 2,
"zip": 94102
}]
}
现在“人员”设置并不是很难,但我在编写EclipseLink OXM元数据时仍然坚持如何使映射工作。我正在寻找一种方法来重新定义人们如何列出,以便我可以获得我的映射。
<xml-element java-attribute="people">
<xml-element java-attribute="id" xml-path="@id" />
<xml-element java-attribute="zip" xml-path="@zip" />
</xml-element>
不是我现在可以做到这一点。所以我在PersonCollectionResponse上创建了一个返回List&gt;的getMappings。它是由人民产生的。现在这不太理想,但还有更糟糕的事情。但是当它被写出来时,地图对象就是这样的字符串......
"mappings": ["{person: 1, zip: 10014}", "{person: 2, zip: 94102}"]
所以我想我的问题就是其中之一...
最糟糕的情况我想我是将我的通用地图集制作成新的类对象并为它们提供架构,但这是我想要避免的。
答案 0 :(得分:0)
目前,MOXy不允许您将单个实例的表示拆分为JSON(或XML)树中的多个位置。请输入您希望看到的支持类型的增强请求:
下面我将演示如何映射部分JSON消息。
PARTIAL ANSWER#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>
PARTIAL ANSWER#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>
DEMO CODE
相同的演示代码可以与两个部分解决方案一起使用:
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);
}
}
DOMAIN MODEL
以下是用于两个部分解决方案的域模型:
的 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-your.html。
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory