2012-07-12 72 views
1

要解決另一個問題,我已經從使用Jersey遷移到EclipseLink MOXy以從JAXB創建的對象模型(由Sun JAXB 2.1.12創建)生成JSON。一個區別我注意到的是輸出如何讓MOXy在一行上輸出以節省空間

澤西輸出

{"artist-list":{"offset":0,"count":1,"artist":[{"score":"100","type":"Group","id":"4302e264-1cf0-4d1f-aca7-2a6f89e34b36","name":"Farming Incident","sort-name":"Incident, Farming","gender":"male","country":"AF","disambiguation":"the real one","ipi-list":{"ipi":["1001","1002"]},"life-span":{"begin":"1999-04","ended":"true"},"tag-list":{"tag":[{"count":5,"name":"thrash"},{"count":11,"name":"güth"}]}}]}} 

的格式,但莫西給

"count" : "1", 
    "offset" : "0", 
    "artist" : [ { 
     "id" : "4302e264-1cf0-4d1f-aca7-2a6f89e34b36", 
     "type" : "Group", 
     "score" : "100", 
     "name" : "Farming Incident", 
     "sort-name" : "Incident, Farming", 
     "gender" : "male", 
     "country" : "AF", 
     "disambiguation" : "the real one", 
     "ipis" : [ "1001", "1002" ], 
     "life-span" : { 
     "begin" : "1999-04", 
     "ended" : "true" 
     }, 
     "tags" : [ { 
     "count" : "5", 
     "name" : "thrash" 
     }, { 
     "count" : "11", 
     "name" : "güth" 
     } ] 
    } ] 
} 

莫西是漂亮多了:) 但移動的原因之一通過Json提供我們的數據是爲了減少傳輸帶寬,因此可以讓MOXy生成所有的一條線路,而且每條線路都沒有額外的空間:?

回答

0

默認情況下,EclipseLink JAXB (MOXy)會將JSON編組爲一行。爲了得到格式化輸出需要設置在Marshaller以下屬性:

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 

package forum11450509; 

public class Root { 

    private String foo; 
    private int bar; 

    public String getFoo() { 
     return foo; 
    } 

    public void setFoo(String foo) { 
     this.foo = foo; 
    } 

    public int getBar() { 
     return bar; 
    } 

    public void setBar(int bar) { 
     this.bar = bar; 
    } 

} 

jaxb.properties

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory 

演示

下面的代碼演示瞭如何指定格式輸出:

package forum11450509; 

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>(2); 
     properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json"); 
     properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false); 
     JAXBContext jc = JAXBContext.newInstance(new Class[] {Root.class}, properties); 

     Root root = new Root(); 
     root.setFoo("ABC"); 
     root.setBar(123); 

     System.out.println("One Line:"); 
     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.marshal(root, System.out); 

     System.out.println("\nFormatted:"); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(root, System.out); 
    } 

} 

輸出

下面是從運行演示代碼的輸出:

One Line 
{"bar":123,"foo":"ABC"} 
Formatted: 
{ 
    "bar" : 123, 
    "foo" : "ABC" 
} 

JAX- RS

MOXyJsonProvider的相同行爲也適用(請參閱:http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html)。

package org.example; 

import java.util.*; 
import javax.ws.rs.core.Application; 
import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider; 

public class CustomerApplication extends Application { 

    @Override 
    public Set<Class<?>> getClasses() { 
     HashSet<Class<?>> set = new HashSet<Class<?>>(2); 
     set.add(MOXyJsonProvider.class); 
     set.add(CustomerService.class); 
     return set; 
    } 

} 

格式化輸出

一號線

默認情況下,當您在您的JAX-RS應用MOXyJsonProvider,輸出將在一行整理

您也可以配置MOXyJsonProvider產生格式化輸出:

package org.example; 

import java.util.*; 
import javax.ws.rs.core.Application; 
import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider; 

public class CustomerApplication extends Application { 

    @Override 
    public Set<Class<?>> getClasses() { 
     HashSet<Class<?>> set = new HashSet<Class<?>>(1); 
     set.add(ExampleService.class); 
     return set; 
    } 

    @Override 
    public Set<Object> getSingletons() { 
     MOXyJsonProvider moxyJsonProvider = new MOXyJsonProvider(); 
     moxyJsonProvider.setFormattedOutput(true); 
    } 

} 
+1

啊應該看到這個我一直在尋找我的InitialContext的方法,而不是我的編組初始化,禁用編組器。JAXB_FORMATTED_OUTPUT,它現在可以運行 – 2012-07-12 18:37:54

+0

@Blaise我有一個問題。我通過在jaxb.properties文件中提供條目javax.xml.bind.context.factory = org.eclipse.persistence.jaxb.JAXBContextFactory來指定moxy作爲JAXB提供程序。我沒有爲json輸出指定moxyjsonprovider。在我的情況下,將會用什麼來提供json輸出?如果我的理解錯誤,請糾正我。 – user2186831 2015-08-11 15:12:07

+0

@ user2186831 - 這取決於。沒有一個適用於所有環境的標準JSON綁定提供程序,因此您將獲得您正在使用的env的默認設置。例如,在GlassFish 4中,缺省值是MOXy:http://blog.bdoughan.com/2013/06/moxy-is-new-default-json-binding.html – 2015-08-13 01:21:44