Search in sources :

Example 1 with HttpMessageConverters

use of org.springframework.boot.autoconfigure.web.HttpMessageConverters in project spring_boot by hryou0922.

the class MvcApplication method fastJsonHttpMessageConverters.

/**
 * 使用fastjson做为spring mvc的json序列化
 * @return
 */
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    fastConverter.setFastJsonConfig(fastJsonConfig);
    HttpMessageConverter<?> converter = fastConverter;
    return new HttpMessageConverters(converter);
}
Also used : FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) HttpMessageConverters(org.springframework.boot.autoconfigure.web.HttpMessageConverters) Bean(org.springframework.context.annotation.Bean)

Example 2 with HttpMessageConverters

use of org.springframework.boot.autoconfigure.web.HttpMessageConverters in project spring_boot by hryou0922.

the class MybatisSpringBoot method fastJsonHttpMessageConverters.

@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
    // 格式化时间
    SerializeConfig mapping = new SerializeConfig();
    mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    // fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    fastJsonConfig.setSerializeConfig(mapping);
    fastConverter.setFastJsonConfig(fastJsonConfig);
    HttpMessageConverter<?> converter = fastConverter;
    return new HttpMessageConverters(converter);
}
Also used : FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) SerializeConfig(com.alibaba.fastjson.serializer.SerializeConfig) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) SimpleDateFormatSerializer(com.alibaba.fastjson.serializer.SimpleDateFormatSerializer) HttpMessageConverters(org.springframework.boot.autoconfigure.web.HttpMessageConverters) Bean(org.springframework.context.annotation.Bean)

Example 3 with HttpMessageConverters

use of org.springframework.boot.autoconfigure.web.HttpMessageConverters in project runelite by runelite.

the class CustomConfiguration method customConverters.

@Bean
public HttpMessageConverters customConverters() {
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
    gsonHttpMessageConverter.setGson(gson);
    // normally you can simply exclude jackson from the classpath to cause
    // Spring to use gson, but minio also depends on jackson
    List<HttpMessageConverter<?>> converters = new RestTemplate().getMessageConverters();
    converters.removeIf(converter -> converter.getClass() == MappingJackson2HttpMessageConverter.class);
    converters.add(gsonHttpMessageConverter);
    return new HttpMessageConverters(false, converters);
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) GsonBuilder(com.google.gson.GsonBuilder) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter) RestTemplate(org.springframework.web.client.RestTemplate) Gson(com.google.gson.Gson) GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter) HttpMessageConverters(org.springframework.boot.autoconfigure.web.HttpMessageConverters) Bean(org.springframework.context.annotation.Bean)

Example 4 with HttpMessageConverters

use of org.springframework.boot.autoconfigure.web.HttpMessageConverters in project spring-cloud-function by spring-cloud.

the class BetterGsonHttpMessageConverter method httpMessageConverters.

@Bean
public HttpMessageConverters httpMessageConverters(Gson gson) {
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    for (HttpMessageConverter<?> converter : new HttpMessageConverters().getConverters()) {
        if (converter instanceof GsonHttpMessageConverter) {
            BetterGsonHttpMessageConverter gsonConverter = new BetterGsonHttpMessageConverter();
            gsonConverter.setGson(gson);
            converters.add(gsonConverter);
        } else {
            converters.add(converter);
        }
    }
    return new HttpMessageConverters(false, converters);
}
Also used : AbstractGenericHttpMessageConverter(org.springframework.http.converter.AbstractGenericHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter) ArrayList(java.util.ArrayList) GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter) HttpMessageConverters(org.springframework.boot.autoconfigure.web.HttpMessageConverters) Bean(org.springframework.context.annotation.Bean)

Aggregations

HttpMessageConverters (org.springframework.boot.autoconfigure.web.HttpMessageConverters)4 Bean (org.springframework.context.annotation.Bean)4 FastJsonConfig (com.alibaba.fastjson.support.config.FastJsonConfig)2 FastJsonHttpMessageConverter (com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter)2 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)2 GsonHttpMessageConverter (org.springframework.http.converter.json.GsonHttpMessageConverter)2 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)1 SimpleDateFormatSerializer (com.alibaba.fastjson.serializer.SimpleDateFormatSerializer)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 ArrayList (java.util.ArrayList)1 AbstractGenericHttpMessageConverter (org.springframework.http.converter.AbstractGenericHttpMessageConverter)1 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)1 RestTemplate (org.springframework.web.client.RestTemplate)1