springmcv配置fastjson依赖
时间: 2023-10-25 22:10:09 浏览: 106
要在Spring MVC中使用fastjson作为JSON序列化和反序列化库,需要进行以下配置:
1. 在pom.xml中添加以下依赖:
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
```
2. 在Spring MVC的配置文件(通常是servlet-context.xml)中添加以下配置:
```xml
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
<property name="objectMapper">
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="fastJsonConfig">
<bean class="com.alibaba.fastjson.support.config.FastJsonConfig">
<property name="serializerFeatures">
<array>
<value>WriteMapNullValue</value>
<value>DisableCircularReferenceDetect</value>
</array>
</property>
</bean>
</property>
</bean>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter"/>
</list>
</property>
</bean>
```
这里使用了`FastJsonHttpMessageConverter`替换了原来的`MappingJackson2HttpMessageConverter`,并且使用了`FastJsonConfig`配置了一些序列化和反序列化的选项。最后将`jsonConverter`添加到`RequestMappingHandlerAdapter`的`messageConverters`列表中,使其生效。
这样就完成了在Spring MVC中配置fastjson的过程。
阅读全文
相关推荐















