解决一对多,多对一无限序列化报错问题

一.原因

当一对多One引用了More的对象,而More又引用了One,相互引用造成无限循环序列化

二.解决方法

可以使用SerializerFeature自定义序列化格式

protected String toJsonString(Object object){
		SerializerFeature[] features = new SerializerFeature[]{
				SerializerFeature.DisableCircularReferenceDetect,
				SerializerFeature.WriteMapNullValue,
				SerializerFeature.WriteNullListAsEmpty,
				SerializerFeature.WriteNullStringAsEmpty,
				SerializerFeature.WriteDateUseDateFormat
				};
		JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
		String jsonStr = JSON.toJSONString(object,features);
		return jsonStr;
	}

猜你喜欢

转载自blog.csdn.net/xm393392625/article/details/83992711