Search in sources :

Example 11 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project fastjson by alibaba.

the class Issue3805 method test.

@Test
public void test() throws Exception {
    String dateFormat = "yyyy";
    TestModel model = new TestModel();
    model.setCreateTime(LocalDateTime.of(2021, 5, 6, 7, 8, 9, 5));
    FastJsonConfig config = new FastJsonConfig();
    config.setDateFormat(dateFormat);
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    converter.setFastJsonConfig(config);
    converter.canRead(TestModel.class, MediaType.APPLICATION_JSON_UTF8);
    converter.canWrite(TestModel.class, MediaType.APPLICATION_JSON_UTF8);
    final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    HttpOutputMessage out = new HttpOutputMessage() {

        public HttpHeaders getHeaders() {
            return new HttpHeaders() {

                private static final long serialVersionUID = 1L;

                @Override
                public MediaType getContentType() {
                    return MediaType.APPLICATION_JSON;
                }
            };
        }

        public OutputStream getBody() throws IOException {
            return byteOut;
        }
    };
    converter.write(model, TestModel.class, MediaType.APPLICATION_JSON_UTF8, out);
    byte[] bytes = byteOut.toByteArray();
    String jsonString = new String(bytes, "UTF-8");
    // System.out.println(jsonString);
    Assert.assertEquals(jsonString, "{\"createTime\":\"2021\"}");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) HttpOutputMessage(org.springframework.http.HttpOutputMessage) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 12 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project fastjson by alibaba.

the class DateFormatPriorityTest method test_priority_01.

// Annotation + FastJsonConfig (Annotation优先)
public void test_priority_01() throws Exception {
    // FastJsonConfig
    FastJsonConfig config = new FastJsonConfig();
    config.setDateFormat("yyyy-MM.dd");
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    converter.setFastJsonConfig(config);
    converter.canRead(VO.class, MediaType.APPLICATION_JSON_UTF8);
    converter.canWrite(VO.class, MediaType.APPLICATION_JSON_UTF8);
    final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    HttpOutputMessage out = new HttpOutputMessage() {

        public HttpHeaders getHeaders() {
            return new HttpHeaders() {

                private static final long serialVersionUID = 1L;

                @Override
                public MediaType getContentType() {
                    return MediaType.APPLICATION_JSON;
                }
            };
        }

        public OutputStream getBody() throws IOException {
            return byteOut;
        }
    };
    VO2 vo = new VO2();
    vo.setDate(calendar.getTime());
    converter.write(vo, VO.class, MediaType.APPLICATION_JSON_UTF8, out);
    byte[] bytes = byteOut.toByteArray();
    String jsonString = new String(bytes, "UTF-8");
    assertEquals("{\"date\":\"1995.10-26\"}", jsonString);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) HttpOutputMessage(org.springframework.http.HttpOutputMessage) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 13 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project Minecraft-Server-WebStore by Ba1oretto.

the class DefaultFastjsonConfig method fastJsonHttpMessageConverter.

@Bean
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    converter.setFastJsonConfig(fastjsonConfig());
    return converter;
}
Also used : FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) Bean(org.springframework.context.annotation.Bean)

Example 14 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project springboot_op by SnailFastGo.

the class FastJsonConfiguration method configureMessageConverters.

/**
 * 修改自定义消息转换器
 * @param converters 消息转换器列表
 */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    // 调用父类的配置
    super.configureMessageConverters(converters);
    // 创建fastJson消息转换器
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    // 处理中文乱码问题
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    fastConverter.setSupportedMediaTypes(fastMediaTypes);
    // 创建配置类
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    // 修改配置返回内容的过滤
    fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty);
    fastConverter.setFastJsonConfig(fastJsonConfig);
    // 将fastjson添加到视图消息转换器列表内
    converters.add(fastConverter);
}
Also used : FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) ArrayList(java.util.ArrayList) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) MediaType(org.springframework.http.MediaType)

Example 15 with FastJsonHttpMessageConverter

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter in project spring-boot by Linda-Tan.

the class RestTemplateConfig method restTemplate.

@Bean("restTemplate")
@ConditionalOnMissingBean({ RestOperations.class, RestTemplate.class })
public RestTemplate restTemplate() {
    // http://rensanning.iteye.com/?page=3
    RestTemplate restTemplate = new RestTemplate(simpleClientHttpRequestFactory());
    restTemplate.getMessageConverters().add(new FastJsonHttpMessageConverter());
    return restTemplate;
}
Also used : RestTemplate(org.springframework.web.client.RestTemplate) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

FastJsonHttpMessageConverter (com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter)42 FastJsonConfig (com.alibaba.fastjson.support.config.FastJsonConfig)33 Bean (org.springframework.context.annotation.Bean)22 MediaType (org.springframework.http.MediaType)17 ArrayList (java.util.ArrayList)16 HttpMessageConverters (org.springframework.boot.autoconfigure.http.HttpMessageConverters)7 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)5 HttpHeaders (org.springframework.http.HttpHeaders)5 HttpOutputMessage (org.springframework.http.HttpOutputMessage)5 RestTemplate (org.springframework.web.client.RestTemplate)4 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)3 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HttpMessageConverters (org.springframework.boot.autoconfigure.web.HttpMessageConverters)2 HttpInputMessage (org.springframework.http.HttpInputMessage)2 GsonHttpMessageConverter (org.springframework.http.converter.json.GsonHttpMessageConverter)2 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)2 SimpleDateFormatSerializer (com.alibaba.fastjson.serializer.SimpleDateFormatSerializer)1