fastjson有一个问题,我不知道是bug,还是我不会用,请高人指点贵人相助句子

spring 3+ fastjson bug 记录 - 推酷
spring 3+ fastjson bug 记录
场景描述:
&使用fastjson的JSON.toJSONString(Domain)的时候,如果Domain中有字段是通过spring proxy出来的,在spring3以上版本会报错,spring3以下不受影响:代理代码如下:
ProxyFactory proxy = new ProxyFactory(Manager);
proxy.addAdvice(Interceptor);
return (VO) proxy.getProxy();
&& 报错如下:
Caused by: com.alibaba.fastjson.JSONException: create asm serializer error, class interface org.springframework.aop.Advisor
at com.alibaba.fastjson.serializer.SerializeConfig.createJavaBeanSerializer(SerializeConfig.java:88)
at com.alibaba.fastjson.serializer.JSONSerializer.getObjectWriter(JSONSerializer.java:455)
at com.alibaba.fastjson.serializer.JSONSerializer.getObjectWriter(JSONSerializer.java:423)
at com.alibaba.fastjson.serializer.JSONSerializer.writeWithFieldName(JSONSerializer.java:371)
at Serializer_6.write1(Unknown Source)
at Serializer_6.write(Unknown Source)
at com.alibaba.fastjson.serializer.JSONSerializer.writeWithFieldName(JSONSerializer.java:373)
at Serializer_2.write1(Unknown Source)
at Serializer_2.write(Unknown Source)
at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:352)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:378)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:366)
Caused by: java.lang.NullPointerException
at com.alibaba.fastjson.util.TypeUtils.isJSONTypeIgnore(TypeUtils.java:952)
at com.alibaba.fastjson.util.TypeUtils.isJSONTypeIgnore(TypeUtils.java:963)
at com.alibaba.fastjson.puteGetters(TypeUtils.java:827)
spring3以后spring-CORE 里面包含了CGLIB,相关类由
net.sf.cglib.proxy.Factory
org.springframework.cglib.proxy.Factory
假设Domain里面的A这个对象,通过A.getInterface[] 可以看到
spring 3: (java.lang.Class&T&[]) [interface org.springframework.aop.SpringProxy, interface org.springframework.aop.framework.Advised, interface org.springframework.cglib.proxy.Factory]
spring 2: (java.lang.Class&T&[]) [interface org.springframework.aop.SpringProxy, interface org.springframework.aop.framework.Advised, interface net.sf.cglib.proxy.Factory]
而在fastjsom:JSONSerializer的 472行(21版本)
for (Class&?& item : clazz.getInterfaces()) {
if (item.getName().equals(&net.sf.cglib.proxy.Factory&)) {
isCglibProxy =
} else if (item.getName().equals(&javassist.util.proxy.ProxyObject&)) {
isJavassistProxy =
&这里会判断失效。导致问题。
已报告fastjson团队,目前1.1.40版本任未修复。
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致在springmvc中解决FastJson循环引用的问题 - 勤奋的asialee的博客 - ITeye技术网站
博客分类:
我们先来看一个例子:
package com.elong.
import java.io.OutputS
import java.util.HashM
import java.util.M
import com.alibaba.fastjson.JSON;
public class Test {
public static void main(String[] args) {
Map&String, Student& maps = new HashMap&String, Student&();
Student s1 = new Student("s1", 16);
maps.put("s1", s1);
maps.put("s2", s1);
byte[] bytes = JSON.toJSONBytes(maps);
System.out.println(new String(bytes));
{"s1":{"age":16,"name":"s1"},"s2":{"$ref":"$.s1"}}
可以看到,这个json如果发到前端是无法使用的,幸好FastJson提供了解决办法,我们来看下,解决办法为禁用循环引用检测,代码如下:
package com.elong.
import java.io.OutputS
import java.util.HashM
import java.util.M
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerF
public class Test {
public static void main(String[] args) {
Map&String, Student& maps = new HashMap&String, Student&();
Student s1 = new Student("s1", 16);
maps.put("s1", s1);
maps.put("s2", s1);
SerializerFeature feature = SerializerFeature.DisableCircularReferenceD
byte[] bytes = JSON.toJSONBytes(maps,feature);
System.out.println(new String(bytes));
输出如下:
{"s1":{"age":16,"name":"s1"},"s2":{"age":16,"name":"s1"}}
问题是如果我们在spring mvc中使用的时候,需要将SerializerFeature注入到MessageConverter里面,
FastJsonHttpMessageConverter
但是SerializerFeature是一个enum类型的,又是一个array,考虑到大部分人对这个不熟悉,直接上代码了。
&bean id="jsonConverter"
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"&
&property name="supportedMediaTypes" value="application/charset=UTF-8"/&
&property name="features"&
&array value-type="com.alibaba.fastjson.serializer.SerializerFeature"&
&value&DisableCircularReferenceDetect&/value&
&/property&
&bean id="DisableCircularReferenceDetect" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"&
&property name="staticField" value="com.alibaba.fastjson.serializer.SerializerFeature.DisableCircularReferenceDetect"&&/property&
浏览 13086
浏览: 706968 次
来自: 北京
感觉LogManager打开了所有的LogSegment(文件 ...
大神 你好 我再使用ueditor 进行编辑的时候发现他会在我 ...

我要回帖

更多关于 高人指点 的文章

 

随机推荐