[英]How to prevent Jackson from outputting pretty print JSON?
我想確保傑克遜生成的JSON文件永遠不會打印出來。 我是一名從事預先存在的項目的初級人員,因此我需要向后工作以找出JSON可以配置為輸出的所有方式。 我可以確認在項目中有0個引用.defaultPrettyPrintingWriter(),以及0個引用.setSerializationConfig,我相信它也可以用來啟用漂亮的打印。
那怎么可能呢? 或者,是否有一種可靠的方法來確保JSON文件不是很好的打印?
根據您使用的Spring版本, MappingJacksonHttpMessageConverter
應該有一個名為prettyPrint
的布爾屬性,以便在序列化JSON時配置打印機。
所以這個XML配置應該可以解決問題(如果您使用的是最新版本的Spring 3)
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
<property name="objectMapper" ref="jacksonObjectMapper" />
<property name="prettyPrint" value="false" />
</bean>
您可以在github上看到引入該屬性的相關提交 。 並且類的主干版本也包含此屬性。 最后這是與前一次提交相關的Spring Jira問題SPR-7201 。
或者您可以嘗試將您的Jackson版本更新為更新版本,其中包括Alexander Ryzhov所述的useDefaultPrettyPrinter
和setPrettyPrinter
方法
最優雅的解決方案是將漂亮/非漂亮的開關放入過濾器,並重用靜態配置對象。 這可以防止無用的垃圾收集。
/**
* 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;
}
}
}
不確定您使用的是哪個版本的Jackson,但在最新版本(1.9.10)中,JsonGenerator的默認行為並不是很好。 打開它的最簡單方法是調用generator.useDefaultPrettyPrinter()
或generator.setPrettyPrinter(new DefaultPrettyPrinter())
。 嘗試搜索useDefaultPrettyPrinter
和setPrettyPrinter
並刪除這些語句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.