我知道如何在默认情况下打印JSON,但我想让用户选择。这就是为什么我想通过QueryParam使其可配置。
这应该打印json(如果没有给出默认值为false):
...test123/res123?pretty=T
...test123/res123?pretty=True
...test123/res123?pretty=t
...test123/res123?pretty=true
如果没有将相同的代码复制到数千个资源,有人有一个好主意吗?我应该和一个消息体作者一起做这个吗?或外向过滤器?
解决方案(感谢Alexey Gavrilov提示):
import java.io.IOException;
import javax.ws.rs.core.MultivaluedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.jaxrs.cfg.EndpointConfigBase;
import com.fasterxml.jackson.jaxrs.cfg.ObjectWriterModifier;
public class IndentingModifier extends ObjectWriterModifier {
private static final Logger LOG = LoggerFactory.getLogger(IndentingModifier.class);
public static boolean doIndent = false;
public final Boolean _indent;
public IndentingModifier() {
this(null);
}
public IndentingModifier(
Boolean indent) {
_indent = indent;
}
@Override
public ObjectWriter modify(
EndpointConfigBase<?> endpoint,
MultivaluedMap<String, Object> responseHeaders,
Object valueToWrite,
ObjectWriter w,
JsonGenerator g) throws IOException {
if (_indent != null) {
if (_indent.booleanValue()) {
LOG.debug("Using default pretty printer, because ident is null.");
g.useDefaultPrettyPrinter();
}
} else {
if (doIndent) {
LOG.debug("Using default pretty printer, because ident is true.");
g.useDefaultPrettyPrinter();
}
}
return w;
}
}
容器过滤器:
import java.io.IOException;
import java.util.List;
import java.util.Map.Entry;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.jaxrs.cfg.ObjectWriterInjector;
@Provider
public class JsonPrettryPrintQueryParamContainerResponseFilter implements javax.ws.rs.container.ContainerResponseFilter {
private static final Logger LOG = LoggerFactory.getLogger(JsonPrettryPrintQueryParamContainerResponseFilter.class);
private static final String QUERY_PARAM_PRETTY = "pretty";
private static final String QUERY_PARAM_T = "t";
private static final String QUERY_PARAM_TRUE = "true";
private static final String QUERY_PARAM_F = "f";
private static final String QUERY_PARAM_False = "false";
@Override
public void filter(
ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
MultivaluedMap<String, String> queryParams = requestContext.getUriInfo().getQueryParameters();
for (Entry<String, List<String>> queryEntry : queryParams.entrySet()) {
if (queryEntry.getKey().equalsIgnoreCase(QUERY_PARAM_PRETTY)) {
String valueOfLastQueryParamIndex = queryEntry.getValue().get(queryEntry.getValue().size() - 1);
LOG.debug(String.format("Found queryPram '%s' with value '%s'.", queryEntry.getKey(),
valueOfLastQueryParamIndex));
switch (valueOfLastQueryParamIndex.toLowerCase()) {
case QUERY_PARAM_T:
ObjectWriterInjector.set(new IndentingModifier(true));
break;
case QUERY_PARAM_TRUE:
ObjectWriterInjector.set(new IndentingModifier(true));
break;
case QUERY_PARAM_F:
ObjectWriterInjector.set(new IndentingModifier(false));
break;
case QUERY_PARAM_False:
ObjectWriterInjector.set(new IndentingModifier(false));
break;
default:
break;
}
break;
}
}
}
}
在扩展的应用程序类run()中:
environment.jersey().register(JsonPrettryPrintQueryParamContainerResponseFilter.class);
答案 0 :(得分:1)
您可以使用ObjectWriterInjector and ObjectWriterModifier根据查询参数自定义资源方法中的对象编写器。
查看Jackson JAX-RS提供程序存储库中的this code sample。