注:私は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)
ここでは、それら@XmlTransient
ことによって、フィールドのカップルのマッピングを解除するには、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構造にJavaモデルをマッピングします。
<?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"
}
}
目的のためのあなたの最終的な解決策は何ですか?それは非常に興味深い話題ですが、何の応答も答えもありません。私は同じ問題に取り組んでいます。私はJacson JsonViewが良い選択だと思います。あなたは紹介を参照することができます。 http://wiki.fasterxml.com/JacksonJsonViews – Dylan
私たちは、jsonで使用したいホワイトリストのプロパティを含む各クラス/ビューの組み合わせに対して少しハッシュセットを作成してから、オブジェクトをObjectMapperに渡し、SimpleBeanPropertyFilter.filterOutAllExceptを使用してjson –
リック。ご協力いただきありがとうございます。非常に便利です。 – Dylan