Search in sources :

Example 6 with MappingJackson2HttpMessageConverter

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));
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) MediaType(org.springframework.http.MediaType) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) SneakyThrows(lombok.SneakyThrows)

Example 7 with MappingJackson2HttpMessageConverter

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;
        }
    });
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 8 with MappingJackson2HttpMessageConverter

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;
        }
    });
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 9 with MappingJackson2HttpMessageConverter

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);
    }
}
Also used : Ordered(org.springframework.core.Ordered) ViewResolver(org.springframework.web.servlet.ViewResolver) NikitaETAGInterceptor(no.arkivlab.hioa.nikita.webapp.web.interceptor.NikitaETAGInterceptor) VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) LocaleResolver(org.springframework.web.servlet.LocaleResolver) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) SessionLocaleResolver(org.springframework.web.servlet.i18n.SessionLocaleResolver) EnableSpringDataWebSupport(org.springframework.data.web.config.EnableSpringDataWebSupport) CacheControl(org.springframework.http.CacheControl) org.springframework.web.servlet.config.annotation(org.springframework.web.servlet.config.annotation) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) TimeUnit(java.util.concurrent.TimeUnit) LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) List(java.util.List) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Locale(java.util.Locale) Optional(java.util.Optional) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) Bean(org.springframework.context.annotation.Bean) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter)

Example 10 with MappingJackson2HttpMessageConverter

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)));
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) RestTemplate(org.springframework.web.client.RestTemplate) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)

Aggregations

MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)122 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)51 ArrayList (java.util.ArrayList)46 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)38 Test (org.junit.jupiter.api.Test)28 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)25 RestTemplate (org.springframework.web.client.RestTemplate)25 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)21 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)20 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)19 HandlerMethod (org.springframework.web.method.HandlerMethod)17 Method (java.lang.reflect.Method)16 Test (org.junit.Test)16 MethodParameter (org.springframework.core.MethodParameter)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 Bean (org.springframework.context.annotation.Bean)14 List (java.util.List)9 HttpEntity (org.springframework.http.HttpEntity)7 MediaType (org.springframework.http.MediaType)6 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)6