use of org.springframework.web.context.request.RequestAttributes in project topcom-cloud by 545314690.
the class MyWebAppConfigurer method extendMessageConverters.
/**
* 添加返回结果缩进支持,如果存在pretty参数,则返回结果添加缩进
* @param converters
*/
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.replaceAll(c -> {
if (c instanceof MappingJackson2HttpMessageConverter) {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(mapper) {
@Override
protected void writePrefix(JsonGenerator generator, Object object) throws IOException {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
if (attributes != null && attributes instanceof ServletRequestAttributes) {
String attribute = ((ServletRequestAttributes) attributes).getRequest().getParameter("pretty");
if (attribute != null) {
generator.setPrettyPrinter(new DefaultPrettyPrinter());
}
}
super.writePrefix(generator, object);
}
};
return converter;
} else {
return c;
}
});
}
Aggregations