नोट: मैं EclipseLink JAXB (MOXy) नेतृत्व और JAXB (JSR-222) विशेषज्ञ समूह का सदस्य हूँ।
मोक्सी जेएक्सबी एनोटेशन के साथ-साथ एक बाहरी बाध्यकारी दस्तावेज़ के आधार पर जेएसओएन-बाध्यकारी प्रदान करता है जो आपको डोमेन मॉडल में वैकल्पिक मैपिंग लागू करने की अनुमति देता है। मैं एक उदाहरण के साथ नीचे प्रदर्शन करेंगे।
मेटाडाटा JAXB एनोटेशन के रूप में
नीचे मानक JAXB एनोटेशन के साथ एक सरल जावा मॉडल मानचित्रण है।
package forum10761762;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
int id;
@XmlElement(name="first-name")
String firstName;
@XmlElement(name="last-name")
String lastName;
}
वैकल्पिक मेटाडाटा # 1 (alternate1.xml)
यहाँ हम उन्हें @XmlTransient
बनाकर क्षेत्रों के एक जोड़े unmap करने के लिए XML मैपिंग दस्तावेज़ का उपयोग करेगा।
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum10761762">
<java-types>
<java-type name="Customer">
<java-attributes>
<xml-transient java-attribute="id"/>
<xml-transient java-attribute="firstName"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
वैकल्पिक मेटाडाटा # 2 (alternate2.xml)
यहाँ हम MOXY के पथ आधारित मैपिंग एक्सटेंशन का उपयोग कर एक अलग JSON संरचना करने के लिए जावा मॉडल नक्शा होगा।
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum10761762">
<java-types>
<java-type name="Customer">
<java-attributes>
<xml-element java-attribute="firstName" xml-path="personalInfo/firstName/text()"/>
<xml-element java-attribute="lastName" xml-path="personalInfo/lastName/text()"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
डेमो कोड
package forum10761762;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
public class Demo {
public static void main(String[] args) throws Exception {
Customer customer = new Customer();
customer.id = 123;
customer.firstName = "Jane";
customer.lastName = "Doe";
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
// Output #1
JAXBContext jc1 = JAXBContext.newInstance(new Class[] {Customer.class}, properties);
marshal(jc1, customer);
// Output #2
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum10761762/alternate1.xml");
JAXBContext jc2 = JAXBContext.newInstance(new Class[] {Customer.class}, properties);
marshal (jc2, customer);
// Output #2
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum10761762/alternate2.xml");
JAXBContext jc3 = JAXBContext.newInstance(new Class[] {Customer.class}, properties);
marshal(jc3, customer);
}
private static void marshal(JAXBContext jc, Object object) throws Exception {
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(object, System.out);
System.out.println();
}
}
आउटपुट
नीचे डेमो कोड को चलने से उत्पादन होता है। उसी ऑब्जेक्ट मॉडल से नोट 3 विभिन्न JSON दस्तावेजों का उत्पादन किया गया था।
{
"id" : 123,
"first-name" : "Jane",
"last-name" : "Doe"
}
{
"last-name" : "Doe"
}
{
"id" : 123,
"personalInfo" : {
"firstName" : "Jane",
"lastName" : "Doe"
}
}
अधिक जानकारी (मेरे ब्लॉग से) के लिए
उद्देश्य के लिए आपका अंतिम समाधान क्या है? यह बहुत ही रोचक विषय है, लेकिन बिना किसी प्रतिक्रिया या उत्तर के। मैं एक ही समस्या से निपट रहा हूँ। मुझे लगता है कि जैक्सन जेसन व्यू अच्छी पसंद है। आप परिचय का उल्लेख कर सकते हैं। http://wiki.fasterxml.com/JacksonJsonViews – Dylan
हम प्रत्येक वर्ग/दृश्य संयोजन के लिए छोटे हैशसेट्स को घायल कर देते हैं जिसमें श्वेतसूची वाले गुण होते हैं जिन्हें हम जेसन में उपयोग करना चाहते थे और फिर ऑब्जेक्ट मैपर को सरल बीनप्रॉपर्टीफिल्टर.फिल्टरऑटएलएक्ससेप्ट के साथ पास करने के लिए जेसन –
रिक। आपकी सहायता के लिए धन्यवाद. बहुत काम का है। – Dylan