SpringMVC整合fastjson

maven 

 <dependency>
 	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.2.7</version>
 </dependency>

spring配置文件

  <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes" value="application/json"/>
                <property name="features">
                    <array>
                        <value>WriteMapNullValue</value>
                        <value>WriteDateUseDateFormat</value>
                    </array>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>


controller--使用

String test(HttpServletRequest request,@RequestBody JSONObject data) throws Exception {      
        System.out.println(data.getString("name"));      
        return "";
    }


可能会报错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0': Cannot create inner bean 'com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter#0' of type [com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter] while setting bean property 'messageConverters' with key [0]; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter] for bean with name 'com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter#0' defined in class path resource [srping/context-web.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: org/springframework/http/converter/AbstractHttpMessageConverter

解决方案:

将fastjson的jar包拷到WEB-INF下的lib文件夹中即可。



©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页