2012-05-25 79 views
7

我們有一組領域類,它們通過使用澤西服務的傑克遜序列化爲json。我們目前正在使用JAXB對類進行註釋(儘管我們並未與此綁定)。這工作正常。但是我們想爲不同的用例提供不同的類的序列化。一個領域模型,多個JSON視圖

  • 網站
  • 移動應用
  • 管理工具
  • 公共API

在每一種情況下,有我們可能會或可能不希望包含在JSON不同視野。例如,管理工具可能需要一些參數來設置數據權限。移動客戶端需要不同於網站的媒體流的URL。該網站具有特定的字段命名約定。

管理Jersey中不同服務端點的json不同映射的最佳做法是什麼?

謝謝!

+0

您的最終解決方案是什麼?這是一個非常有趣的話題,但爲什麼沒有任何迴應或答案。我正在處理同樣的問題。我認爲傑森JsonView是不錯的選擇。你可以參考介紹。 http://wiki.fasterxml.com/JacksonJsonViews – Dylan

+1

我們最終爲包含我們想在json中使用的白名單屬性的每個類/視圖組合創建了一些HashSets,然後使用SimpleBeanPropertyFilter.filterOutAllExcept將對象傳遞給ObjectMapper並創建了ObjectMapper json –

+0

Rick。謝謝你的幫助。這非常有用。 – Dylan

回答

4

備註:我是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" 
    } 
} 

更多信息(從我的博客)

+0

這很好,但它是否能與澤西特別合作? –

+0

@RickMangi - 它將與任何JAX-RS實現一起工作。澤西島和MOXy團隊緊密合作:https://github.com/jersey/jersey/tree/master/examples/json-moxy –