Search in sources :

Example 1 with FastJsonHttpMessageConverter4

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4 in project sinsim by WilsonHu.

the class WebMvcConfig method configureMessageConverters.

// 使用阿里 FastJson 作为JSON MessageConverter
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
    FastJsonConfig config = new FastJsonConfig();
    // 保留空的字段:String null -> "",Number null -> 0,DisableCircularReferenceDetect --> 防止循环引用
    config.setSerializerFeatures(SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero, SerializerFeature.DisableCircularReferenceDetect);
    converter.setFastJsonConfig(config);
    converter.setDefaultCharset(Charset.forName("UTF-8"));
    converters.add(converter);
}
Also used : FastJsonHttpMessageConverter4(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4) FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig)

Example 2 with FastJsonHttpMessageConverter4

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4 in project spring-cloud by Rogge666.

the class WebMvcConfigurer method configureMessageConverters.

// 使用阿里 FastJson 作为JSON MessageConverter
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
    FastJsonConfig config = new FastJsonConfig();
    // 保留空的字段
    config.setSerializerFeatures(// 保留空的字段
    SerializerFeature.WriteMapNullValue, // String null -> ""
    SerializerFeature.WriteNullStringAsEmpty, // 使枚举返回ordinal
    SerializerFeature.WriteEnumUsingToString, // Number null -> 0
    SerializerFeature.WriteNullNumberAsZero);
    converter.setFastJsonConfig(config);
    converter.setDefaultCharset(Charset.forName("UTF-8"));
    converters.add(converter);
}
Also used : FastJsonHttpMessageConverter4(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4) FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig)

Example 3 with FastJsonHttpMessageConverter4

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4 in project spring-cloud by Rogge666.

the class WebMvcConfigurer method configureMessageConverters.

// 使用阿里 FastJson 作为JSON MessageConverter
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
    FastJsonConfig config = new FastJsonConfig();
    // 保留空的字段
    config.setSerializerFeatures(// 保留空的字段
    SerializerFeature.WriteMapNullValue, // String null -> ""
    SerializerFeature.WriteNullStringAsEmpty, // 使枚举返回ordinal
    SerializerFeature.WriteEnumUsingToString, // Number null -> 0
    SerializerFeature.WriteNullNumberAsZero);
    converter.setFastJsonConfig(config);
    converter.setDefaultCharset(Charset.forName("UTF-8"));
    converters.add(converter);
}
Also used : FastJsonHttpMessageConverter4(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4) FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig)

Example 4 with FastJsonHttpMessageConverter4

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

the class FastJsonHttpMessageConverter4Test method test_1.

public void test_1() throws Exception {
    FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
    Assert.assertNotNull(converter.getFastJsonConfig());
    converter.setFastJsonConfig(new FastJsonConfig());
    converter.canRead(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
    converter.canWrite(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
    Method method1 = FastJsonHttpMessageConverter4.class.getDeclaredMethod("supports", Class.class);
    method1.setAccessible(true);
    method1.invoke(converter, int.class);
    HttpInputMessage input = new HttpInputMessage() {

        public HttpHeaders getHeaders() {
            // TODO Auto-generated method stub
            return null;
        }

        public InputStream getBody() throws IOException {
            return new ByteArrayInputStream("{\"id\":123}".getBytes(Charset.forName("UTF-8")));
        }
    };
    VO vo = (VO) converter.read(VO.class, VO.class, input);
    Assert.assertEquals(123, vo.getId());
    final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    HttpOutputMessage out = new HttpOutputMessage() {

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

        public OutputStream getBody() throws IOException {
            return byteOut;
        }
    };
    converter.write(vo, VO.class, MediaType.TEXT_PLAIN, out);
    byte[] bytes = byteOut.toByteArray();
    Assert.assertEquals("{\"id\":123}", new String(bytes, "UTF-8"));
    Method method2 = FastJsonHttpMessageConverter4.class.getDeclaredMethod("readInternal", Class.class, HttpInputMessage.class);
    method2.setAccessible(true);
    method2.invoke(converter, VO.class, input);
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) FastJsonHttpMessageConverter4(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4) HttpHeaders(org.springframework.http.HttpHeaders) FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpOutputMessage(org.springframework.http.HttpOutputMessage) Method(java.lang.reflect.Method) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with FastJsonHttpMessageConverter4

use of com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4 in project new-cloud by xie-summer.

the class WebMvcConfigurer method configureMessageConverters.

// 使用阿里 FastJson 作为JSON MessageConverter
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
    FastJsonConfig config = new FastJsonConfig();
    config.setSerializerFeatures(// 保留空的字段
    SerializerFeature.WriteMapNullValue, // String null -> ""
    SerializerFeature.WriteNullStringAsEmpty, // Number null -> 0
    SerializerFeature.WriteNullNumberAsZero);
    converter.setFastJsonConfig(config);
    converter.setDefaultCharset(Charset.forName("UTF-8"));
    converters.add(converter);
}
Also used : FastJsonHttpMessageConverter4(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4) FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig)

Aggregations

FastJsonConfig (com.alibaba.fastjson.support.config.FastJsonConfig)12 FastJsonHttpMessageConverter4 (com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4)12 ValueFilter (com.alibaba.fastjson.serializer.ValueFilter)2 Bean (org.springframework.context.annotation.Bean)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Method (java.lang.reflect.Method)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpInputMessage (org.springframework.http.HttpInputMessage)1 HttpOutputMessage (org.springframework.http.HttpOutputMessage)1