在ssm项目中使用fastjson(2.0.21)如何,如何通过springmvc配置文件全局关闭循环引用
小魔女参考了bing和GPT部分内容调写:
在使用fastjson的ssm项目中,可以通过springmvc的配置文件全局关闭循环引用。首先在springmvc的配置文件中添加fastjson的解析器:
<!-- 引入fastjson的解析器 -->
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="features">
<array>
<value>WriteMapNullValue</value>
<value>WriteNullListAsEmpty</value>
<value>WriteNullStringAsEmpty</value>
<value>WriteNullNumberAsZero</value>
<value>WriteNullBooleanAsFalse</value>
</array>
</property>
<property name="serializerFeatures">
<array>
<value>DisableCircularReferenceDetect</value>
</array>
</property>
</bean>
其中,DisableCircularReferenceDetect就是用来关闭循环引用的,只要在配置文件中添加上这一句,就可以全局关闭循环引用。
回答不易,记得采纳呀。
该回答引用ChatGPT
这里通过设置 DisableCircularReferenceDetect 来关闭全局循环引用检测。同时,需要将 FastJsonConfig bean 与 FastJsonHttpMessageConverter bean 关联起来,这样 FastJsonHttpMessageConverter 才能使用 FastJsonConfig 的配置信息。
<bean class="com.alibaba.fastjson.support.config.FastJsonConfig">
<property name="features">
<array>
<value>DisableCircularReferenceDetect</value>
</array>
</property>
</bean>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="fastJsonConfig">
<ref bean="fastJsonConfig"/>
</property>
<property name="supportedMediaTypes">
<array>
<value>application/json;charset=UTF-8</value>
</array>
</property>
</bean>
fastjson2相关的坑还有很多,建议还是去用fastjson1吧。要不然遇见问题你都得去扒源码
虽然但是,我还是在fastjson2的源码里找到了解决办法
https://github.com/alibaba/fastjson2/blob/main/docs/features_cn.md
代码如下
@Configuration
@EnableWebMvc
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
//自定义配置...
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
config.setReaderFeatures(JSONReader.Feature.FieldBased, JSONReader.Feature.SupportArrayToBean);
// 其他相关features参考 https://github.com/alibaba/fastjson2/blob/main/docs/features_cn.md 去配置
config.setWriterFeatures(JSONWriter.Feature.WriteMapNullValue, JSONWriter.Feature.ReferenceDetection);
converter.setFastJsonConfig(config);
converter.setDefaultCharset(StandardCharsets.UTF_8);
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
converters.add(0, converter);
}
}
你可以通过在Spring MVC的配置文件中配置MappingJackson2HttpMessageConverter的features来关闭fastjson的循环引用。
具体来说,可以在Spring MVC的配置文件中添加以下配置:
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="features">
<array>
<util:constant static-field="com.alibaba.fastjson.serializer.SerializerFeature.DisableCircularReferenceDetect"/>
</array>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
这里通过设置DisableCircularReferenceDetect来关闭fastjson的循环引用。
另外,也可以通过在代码中使用注解@JSONField(serialize=false)来避免某个属性被序列化,从而避免循环引用。例如:
public class User {
private String name;
@JSONField(serialize=false)
private List<User> friends;
// getters and setters
}
这里通过@JSONField(serialize=false)注解来避免friends属性被序列化。
该回答引用GPTᴼᴾᴱᴺᴬᴵ
Fastjson 支持通过 SerializerFeature 来关闭循环引用。具体来说,您可以通过设置 SerializerFeature.DisableCircularReferenceDetect 属性来关闭循环引用。
在 Spring MVC 中,您可以通过在配置文件中配置一个 MappingJackson2HttpMessageConverter 实例,并在其中设置 ObjectMapper 的相关属性,以达到全局关闭循环引用的效果。具体步骤如下:
1.在 Spring MVC 的配置文件中配置一个 MappingJackson2HttpMessageConverter 实例:
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="features">
<array>
<value>DisableCircularReferenceDetect</value>
</array>
</property>
</bean>
</property>
</bean>
2.在 FastJsonHttpMessageConverter 中设置 SerializerFeature.DisableCircularReferenceDetect 属性:
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="features">
<array>
<value>DisableCircularReferenceDetect</value>
</array>
</property>
</bean>
通过以上配置,Fastjson 将全局关闭循环引用,可以避免在序列化过程中出现循环引用导致的 StackOverflowError 异常。
您可以按照以下步骤在SSM项目中使用Fastjson,并在Spring MVC配置文件中全局关闭循环引用:
1.添加Fastjson依赖
在您的SSM项目中,您可以在Maven或Gradle构建文件中添加Fastjson依赖。例如,如果您正在使用Maven,则可以将以下代码添加到pom.xml文件中:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.21</version>
</dependency>
2.在Spring MVC配置文件中配置FastjsonHttpMessageConverter
您需要在Spring MVC配置文件中配置FastjsonHttpMessageConverter以将Fastjson与Spring MVC集成。以下是一个示例配置文件:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="fastJsonConfig">
<bean class="com.alibaba.fastjson.support.config.FastJsonConfig">
<!-- 设置Fastjson的序列化参数 -->
<property name="serializeFilters">
<list>
<!-- 这里可以添加各种自定义的SerializeFilter -->
</list>
</property>
<property name="serializerFeatures">
<list>
<value>DisableCircularReferenceDetect</value>
</list>
</property>
</bean>
</property>
</bean>
</list>
</property>
</bean>
在上面的配置文件中,我们配置了一个RequestMappingHandlerAdapter bean,它包含一个FastjsonHttpMessageConverter bean。我们通过设置FastJsonConfig bean中的serializerFeatures属性来禁用循环引用检测。
3.测试
完成上述配置后,您可以使用Fastjson序列化和反序列化对象。在序列化和反序列化对象时,Fastjson会自动使用配置文件中定义的FastjsonHttpMessageConverter。
希望这可以帮助您在SSM项目中使用Fastjson,并在Spring MVC配置文件中全局关闭循环引用。
在 SSM 项目中使用 fastjson 可以通过以下步骤:
在 pom.xml 文件中添加 fastjson 依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.21</version>
</dependency>
配置 SpringMVC 的 MappingJackson2HttpMessageConverter,使用 fastjson 作为消息转换器:
<mvc:annotation-driven>
<mvc:message-converters>
<!-- 使用 fastjson 作为消息转换器 -->
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
如果需要全局关闭循环引用,可以在 FastJsonHttpMessageConverter 上设置 SerializerFeature.DisableCircularReferenceDetect:
<mvc:annotation-driven>
<mvc:message-converters>
<!-- 使用 fastjson 作为消息转换器 -->
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<!-- 全局关闭循环引用 -->
<property name="features">
<set>
<value>DisableCircularReferenceDetect</value>
</set>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
可以通过在配置文件中设置fastjson.parser.autoTypeSupport=false
来全局禁用循环引用检测。此外,您还可以在需要进行序列化/反序列化的对象上使用@JSONType(orders = {"id","name","age"}, ignores = {"password"})
注解,手动控制序列化/反序列化过程中的字段顺序和忽略字段。
在ssm项目中使用fastjson(2.0.21)如何,如何通过springmvc配置文件全局关闭循环引用。
具体解决步骤如下,希望可以帮助到你,望采纳,感谢。
在 SSM 项目中使用 Fastjson,可以通过以下步骤进行配置:
1.在 pom.xml 文件中添加 Fastjson 依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.21</version>
</dependency>
2.在 Spring MVC 的配置文件中添加以下配置:
这里通过配置 FastJsonHttpMessageConverter 来使用 Fastjson 进行 JSON 数据的序列化和反序列化,并在 FastJsonConfig 中设置 DisableCircularReferenceDetect 来关闭循环引用检测。
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="fastJsonConfig" ref="fastJsonConfig" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="fastJsonConfig" class="com.alibaba.fastjson.support.config.FastJsonConfig">
<property name="serializerFeatures">
<list>
<value>DisableCircularReferenceDetect</value>
</list>
</property>
</bean>
3.如果需要在控制器中返回 JSON 数据,可以使用 @ResponseBody 注解来指定返回的数据格式为 JSON。例如:
注意:要记得创建User实体类哦。
在这个例子中,控制器返回一个 User 对象,由于使用了 @ResponseBody 注解,Spring MVC 会将该对象序列化为 JSON 数据返回给客户端。
@RequestMapping("/get_user_info")
@ResponseBody
public User getUserInfo() {
User user = new User();
user.setId(1);
user.setName("Jack");
return user;
}
4.通过以上步骤进行配置,就可以在 SSM 项目中使用 Fastjson 并关闭循环引用检测了。
在 fastjson 中关闭循环引用可以通过设置 SerializerFeature.DisableCircularReferenceDetect 来实现。示例代码如下:
plaintext
Copy code
User user = new User();
List<User> friends = new ArrayList<>();
User friend1 = new User();
friend1.setName("Tom");
User friend2 = new User();
friend2.setName("Jerry");
friends.add(friend1);
friends.add(friend2);
user.setName("Alice");
user.setFriends(friends);
friend1.setFriends(Collections.singletonList(user));
friend2.setFriends(Collections.singletonList(user));
String jsonString = JSON.toJSONString(user, SerializerFeature.DisableCircularReferenceDetect);
System.out.println(jsonString);
在上面的代码中,我们创建了一个 User 对象,并设置了他的朋友列表。然后,我们将 Alice 作为 friend1 和 friend2 的朋友。这样就形成了循环引用。最后,我们通过设置 SerializerFeature.DisableCircularReferenceDetect 来关闭循环引用检测,将 user 对象转换为 JSON 字符串并输出。输出结果如下:
plaintext
Copy code
{"friends":[{"friends":[{"friends":[],"name":"Alice"}],"name":"Tom"},{"friends":[{"friends":[],"name":"Alice"}],"name":"Jerry"}],"name":"Alice"}
可以看到,输出结果中没有循环引用。
以下答案由GPT-3.5大模型与博主波罗歌共同编写:
在fastjson中,可以通过设置 SerializerFeature.DisableCircularReferenceDetect 属性来关闭循环引用的处理。
做法如下:
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Autowired
private FastJsonHttpMessageConverter fastJsonHttpMessageConverter;
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(fastJsonHttpMessageConverter);
}
}
这样就可以在SpringMVC项目中全局禁用fastjson的循环引用处理了。
如果我的回答解决了您的问题,请采纳!
可以通过在配置文件中设置fastjson.parser.autoTypeSupport=false来全局禁用循环引用检测。此外,您还可以在需要进行序列化/反序列化的对象上使用@JSONType(orders = {"id","name","age"}, ignores = {"password"})注解,手动控制序列化/反序列化过程中的字段顺序和忽略字段。