2012-08-07 133 views
6

我想確保傑克遜生成的JSON文件永遠不會很漂亮。我是一名初級項目的初級工程師,所以我需要向後工作以找出JSON可以配置爲輸出爲漂亮打印的所有方式。我可以確認在項目中有0個對.defaultPrettyPrintingWriter()的引用,還有0個對.setSerializationConfig的引用,我相信它們也可以用來啓用漂亮的打印。如何防止傑克遜輸出漂亮的打印JSON?

那麼這又怎麼可能呢?或者,有沒有一種確保JSON文件打印效果不錯的方法?

+0

您是否在使用任何bean或應用程序服務器?你可能想在配置文件中檢查'prettyPrint'屬性。 – 2012-08-07 18:47:19

+0

更新:我在我正在開發的項目中發現了ObjectMapper的幾個實例。如果我寫了類似 OutputMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT,false); 然後我會得到我要找的非漂亮的打印JSON,對吧? – tamuren 2012-08-07 18:47:39

+0

*「OutputMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT,false);然後我會得到我要找的非漂亮的打印JSON,對吧?」**理論上,是的,應該使ObjectMapper的該實例不漂亮的印刷品。儘管如此,仍然可以通過其他方式創建ObjectMapper。 – 2012-08-07 18:50:37

回答

2

根據您使用的Spring的版本,MappingJacksonHttpMessageConverte‌​r應該有一個名爲prettyPrint的布爾屬性來在序列化JSON時配置打印機。

所以這個XML配置應該做的伎倆(如果你使用的是最近的彈簧3版)

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAda‌​pter"> 
    <property name="messageConverters"> 
    <list> 
     <ref bean="jsonConverter" /> 
    </list> 
    </property> 
</bean> 

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverte‌​r"> 
    <property name="supportedMediaTypes" value="application/json" /> 
    <property name="objectMapper" ref="jacksonObjectMapper" /> 
    <property name="prettyPrint" value="false" /> 
</bean> 

你可以看到在github上引入屬性的related commit。和類的trunk version也包括這個屬性。最後,這是與之前提交相關的Spring Jira問題SPR-7201

或者你可以嘗試更新您的傑克遜的版本,以更近的一個包含useDefaultPrettyPrintersetPrettyPrinter方法由亞歷山大Ryzhov提到

0

不確定您使用的是哪個版本的Jackson,但在最新版本(1.9.10)中,JsonGenerator的默認行爲沒有漂亮的打印效果。打開它的最簡單方法是致電generator.useDefaultPrettyPrinter()generator.setPrettyPrinter(new DefaultPrettyPrinter())。嘗試搜索useDefaultPrettyPrintersetPrettyPrinter並刪除這些語句。

+0

該項目使用的版本是1.3。我在項目中搜索useDefaultPrettyPrinter和setPrettyPrinter,無濟於事 – tamuren 2012-10-25 13:04:37

1

最優雅的解決方案是把開關漂亮的/不漂亮過濾器,並重新使用靜態配置對象。這可以防止無用的垃圾收集。

/** 
* Evaluate the "pretty" query parameter. If given without any value, or with "true": pretty JSON output. 
* E.g. /test?pretty or /test?pretty=true 
* Otherwise output without any formatting. 
* 
* @see https://stackoverflow.com/questions/10532217/jax-rs-json-pretty-output 
*/ 
@Provider 
@Produces(MediaType.APPLICATION_JSON) 
public class RestPrettyJsonFilter implements ContainerResponseFilter { 

    public static final String QUERYPARAM_PRETTY = "pretty"; 

    private static final IndentingModifier INDENTING_MODIFIER_PRETTY = new IndentingModifier(true); 
    private static final IndentingModifier INDENTING_MODIFIER_NOT_PRETTY = new IndentingModifier(false); 

    @Override 
    public void filter(ContainerRequestContext reqCtx, ContainerResponseContext respCtx) throws IOException { 

    boolean pretty = false; 

    UriInfo uriInfo = reqCtx.getUriInfo(); 
    //log.info("prettyFilter: "+uriInfo.getPath()); 

    MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters(); 
    if(queryParameters.containsKey(QUERYPARAM_PRETTY)) { 
     // Pretty query parameter is present 
     String prettyParam = queryParameters.getFirst(QUERYPARAM_PRETTY); 

     // Pretty is present without any value, or value is set to "true"? 
     if (prettyParam == null || "".equals(prettyParam) || "true".equals(prettyParam)) { 
     pretty = true; 
     } 
    } 

    // Prevent recreation of objects over and over again 
    //ObjectWriterInjector.set(new IndentingModifier(pretty)); 
    if (pretty) { 
     ObjectWriterInjector.set(INDENTING_MODIFIER_PRETTY); 
    } else { 
     ObjectWriterInjector.set(INDENTING_MODIFIER_NOT_PRETTY); 
    } 
    } 

    /** 
    * Used to switch on/off pretty output for each response. 
    */ 
    public static class IndentingModifier extends ObjectWriterModifier { 

    private final boolean indent; 

    /** Minimal pretty printer is not printing pretty. */ 
    private final static PrettyPrinter NOT_PRETTY_PRINTER = new com.fasterxml.jackson.core.util.MinimalPrettyPrinter(); 

    public IndentingModifier(boolean indent) { 
     this.indent = indent; 
    } 

    /* (non-Javadoc) 
    * @see com.fasterxml.jackson.jaxrs.cfg.ObjectWriterModifier#modify(com.fasterxml.jackson.jaxrs.cfg.EndpointConfigBase, javax.ws.rs.core.MultivaluedMap, java.lang.Object, com.fasterxml.jackson.databind.ObjectWriter, com.fasterxml.jackson.core.JsonGenerator) 
    */ 
    @Override 
    public ObjectWriter modify(EndpointConfigBase<?> endpointConfigBase, MultivaluedMap<String, Object> multivaluedMap, Object o, ObjectWriter objectWriter, JsonGenerator jsonGenerator) throws IOException { 
     if(indent) { 
     // Pretty 
     jsonGenerator.useDefaultPrettyPrinter(); 
     } else { 
     // Not pretty 
     jsonGenerator.setPrettyPrinter(NOT_PRETTY_PRINTER); 
     } 
     return objectWriter; 
    } 
    } 
}