[alibaba/fastjson]设置全局转换器,Enum输出ToString报错

2024-08-28 226 views
5

需要转换的对象包含Enum类型时,配置WriteEnumUsingToString时异常。错误提示:message: "com.alibaba.fastjson.serializer.JavaBeanSerializer.processValue(Lcom/alibaba/fastjson/serializer/JSONSerializer;Lcom/alibaba/fastjson/serializer/BeanContext;Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;Ljava/lang/Integer;"

回答

1

遇到相同错误提示Caused by: java.lang.NoSuchMethodError: com.alibaba.fastjson.serializer.JavaBeanSerializer.processValue(Lcom/alibaba/fastjson/serializer/JSONSerializer;Lcom/alibaba/fastjson/serializer/BeanContext;Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;Ljava/lang/Integer; at com.alibaba.fastjson.serializer.ASMSerializer_1_Trptreportconfig.writeNormal(Unknown Source) at com.alibaba.fastjson.serializer.ASMSerializer_1_Trptreportconfig.write(Unknown Source) at com.alibaba.fastjson.serializer.MapSerializer.write(MapSerializer.java:271) at com.alibaba.fastjson.serializer.MapSerializer.write(MapSerializer.java:44)

验证发现,在tomcat上没有此报错,在weblogic上有此报错

0

枚举类怎么搞啊?大佬,我也是报错,一个对象里面有个对象,对象里面有个枚举,然后 JSONObject.parseObject 转换成对象报错

1

创建一个 FastJsonHttpMessageConverter 的bean; 在这个bean中再创建一个 FastJsonConfig ,调用setSerializerFeatures,把“SerializerFeature.WriteEnumUsingToString“放进去; 最后把FastJsonConfig通过FastJsonHttpMessageConverter的setFastJsonConfig方法放进去。

@Bean public FastJsonHttpMessageConverter jsonConverter() { FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); FastJsonConfig config = new FastJsonConfig(); config.setSerializerFeatures( SerializerFeature.WriteNullBooleanAsFalse , SerializerFeature.WriteNullNumberAsZero , SerializerFeature.WriteNullStringAsEmpty , SerializerFeature.WriteMapNullValue , SerializerFeature.WriteNonStringKeyAsString , SerializerFeature.DisableCircularReferenceDetect // , SerializerFeature.WriteEnumUsingName //设置Enum输出ToString内容 , SerializerFeature.WriteEnumUsingToString ); converter.setFastJsonConfig(config); return converter; }