備註:我是EclipseLink JAXB (MOXy)的領導者和JAXB (JSR-222)專家組的成員。
MOXy提供基於JAXB註釋的JSON綁定以及允許您將替代映射應用於域模型的外部綁定文檔。我將在下面舉例說明。
元數據JAXB批註
下面是一個標準的JAXB註解的簡單Java模型映射。
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)
在這裏,我們將使用XML映射文檔,使它們@XmlTransient
取消映射幾個領域。
<?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)
這裏,我們將使用映射莫西的基於路徑映射擴展Java模型到不同的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"
}
}
更多信息(從我的博客)
您的最終解決方案是什麼?這是一個非常有趣的話題,但爲什麼沒有任何迴應或答案。我正在處理同樣的問題。我認爲傑森JsonView是不錯的選擇。你可以參考介紹。 http://wiki.fasterxml.com/JacksonJsonViews – Dylan
我們最終爲包含我們想在json中使用的白名單屬性的每個類/視圖組合創建了一些HashSets,然後使用SimpleBeanPropertyFilter.filterOutAllExcept將對象傳遞給ObjectMapper並創建了ObjectMapper json –
Rick。謝謝你的幫助。這非常有用。 – Dylan