当前位置: 首页>>代码示例>>Java>>正文


Java FastJsonHttpMessageConverter4.setFastJsonConfig方法代码示例

本文整理汇总了Java中com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4.setFastJsonConfig方法的典型用法代码示例。如果您正苦于以下问题:Java FastJsonHttpMessageConverter4.setFastJsonConfig方法的具体用法?Java FastJsonHttpMessageConverter4.setFastJsonConfig怎么用?Java FastJsonHttpMessageConverter4.setFastJsonConfig使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4的用法示例。


在下文中一共展示了FastJsonHttpMessageConverter4.setFastJsonConfig方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: configureMessageConverters

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4; //导入方法依赖的package包/类
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
    FastJsonConfig config = new FastJsonConfig();
    config.setSerializerFeatures(SerializerFeature.WriteMapNullValue,//保留空的字段
            SerializerFeature.WriteNullStringAsEmpty,//String null -> ""
            SerializerFeature.WriteNullNumberAsZero);//Number null -> 0
    converter.setFastJsonConfig(config);
    converter.setDefaultCharset(Charset.forName("UTF-8"));
    converters.add(converter);
}
 
开发者ID:jeikerxiao,项目名称:SpringBootStudy,代码行数:12,代码来源:WebMvcConfig.java

示例2: fastJsonHttpMessageConverter

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4; //导入方法依赖的package包/类
@Bean
@ConditionalOnMissingBean({ FastJsonHttpMessageConverter4.class })
//当没有注册这个类时,自动注册Bean
public FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter() {
    FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();

    //使用最新的官方推荐配置对象的方式来注入fastjson的序列化特征
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(
            SerializerFeature.WriteDateUseDateFormat,
            SerializerFeature.WriteNullListAsEmpty,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullBooleanAsFalse,
            SerializerFeature.WriteNullStringAsEmpty
     );
    
    //添加对json值的过滤,因为像移动APP,服务端在传json值时最好不要传null,而是使用“”,这是一个演示
    ValueFilter valueFilter = new ValueFilter() {//5
        //o 是class
        //s 是key值
        //o1 是value值
        public Object process(Object o, String s, Object o1) {
            if (null == o1) {
                o1 = "";
            }
            return o1;
        }
    };
    fastJsonConfig.setSerializeFilters(valueFilter);

    converter.setFastJsonConfig(fastJsonConfig);

    return converter;
}
 
开发者ID:qjx378,项目名称:wenku,代码行数:35,代码来源:FastJsonHttpMessageConvertersConfiguration.java

示例3: registry

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4; //导入方法依赖的package包/类
@Override
public void registry(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter4();
    ArrayList<MediaType> arrayList = new ArrayList<MediaType>() {{
        add(MediaType.APPLICATION_JSON_UTF8);
        add(MediaType.valueOf("text/html;charset=UTF-8"));
        add(MediaType.MULTIPART_FORM_DATA);
    }};
    fastJsonHttpMessageConverter.setSupportedMediaTypes(arrayList);
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.QuoteFieldNames,
            SerializerFeature.DisableCircularReferenceDetect);
    fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
    converters.add(0, fastJsonHttpMessageConverter);
}
 
开发者ID:ismartx,项目名称:summer,代码行数:16,代码来源:DefaultFastJsonMessageConverterRegister.java

示例4: test_1

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4; //导入方法依赖的package包/类
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);
	}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:54,代码来源:FastJsonHttpMessageConverter4Test.java


注:本文中的com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4.setFastJsonConfig方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。