2012-07-12 1 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.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 

데모

: 형식화 된 출력을 얻으려면 당신은 Marshaller에서 다음 속성을 설정해야합니다

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을 포함 기본적으로

, 출력은 한 줄에 정렬 화합니다

을 구성 할 수도 있습니다.형식화 된 출력을 생성하려면 :

+1

Ah는 내가 본 marshaller 초기화 대신 비활성화 된 Marshaller를 사용하여 initialcontext 메소드를 보았습니다.JAXB_FORMATTED_OUTPUT 이제 작동합니다. –

+0

@Blaise 질문이 있습니다. jaxb.properties 파일에 javax.xml.bind.context.factory = org.eclipse.persistence.jaxb.JAXBContextFactory 항목을 지정하여 JAXB Provider로 moxy를 지정했습니다. json 출력을 위해 moxyjsonprovider를 지정하지 않았습니다. 필자의 경우, json 출력을 제공하는 데 사용되는 것은 무엇입니까? 제 이해가 잘못되면 저를 시정하십시오. – user2186831

+0

@ user2186831 - 다릅니다. 모든 환경에 대해 표준 JSON 바인딩 공급자가 없으므로 사용중인 env의 기본값을 가져옵니다. GlassFish 4에서 기본값은 MOXy입니다. http://blog.bdoughan.com/2013/06/moxy-is-new-default-json-binding.html –

관련 문제