use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project cas by apereo.
the class JsonUtils method render.
/**
* Render model and view.
*
* @param model the model
* @param response the response
*/
@SneakyThrows
public static void render(final Object model, final HttpServletResponse response) {
final MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
jsonConverter.setPrettyPrint(true);
final MediaType jsonMimeType = MediaType.APPLICATION_JSON;
jsonConverter.write(model, jsonMimeType, new ServletServerHttpResponse(response));
}
use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter 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;
}
});
}
use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter 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;
}
});
}
use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project nikita-noark5-core by HiOA-ABI.
the class AppWebMvcConfiguration method extendMessageConverters.
/**
* Allows us to pretty print JSON/XML output. CLI tools like curl may not prettyprint for you
* so this allows us to format the api output so that any developer testing the system gets nicely
* formatted output.
* However the client should be using Accept: application/json or application/xml when connecting
*/
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
final Optional<HttpMessageConverter<?>> jsonConverterFound = converters.stream().filter(c -> c instanceof MappingJackson2HttpMessageConverter).findFirst();
if (jsonConverterFound.isPresent()) {
final AbstractJackson2HttpMessageConverter converter = (AbstractJackson2HttpMessageConverter) jsonConverterFound.get();
converter.getObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
// Convert timestamps to readable text strings
converter.getObjectMapper().configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
converter.getObjectMapper().enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
final Optional<HttpMessageConverter<?>> xmlConverterFound = converters.stream().filter(c -> c instanceof MappingJackson2XmlHttpMessageConverter).findFirst();
if (jsonConverterFound.isPresent()) {
final MappingJackson2XmlHttpMessageConverter converter = (MappingJackson2XmlHttpMessageConverter) xmlConverterFound.get();
converter.getObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
converter.getObjectMapper().enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
}
use of org.springframework.http.converter.json.MappingJackson2HttpMessageConverter in project geode by apache.
the class RestInterfaceJUnitTest method createRestTemplate.
protected RestTemplate createRestTemplate() {
MappingJackson2HttpMessageConverter httpMessageConverter = new MappingJackson2HttpMessageConverter();
httpMessageConverter.setObjectMapper(getObjectMapper());
return setErrorHandler(new RestTemplate(Collections.<HttpMessageConverter<?>>singletonList(httpMessageConverter)));
}
Aggregations