SpringMVC 4.2.4怎么样整合Fastjson?网上找的配置都不行

J2EE 码拜 8年前 (2016-05-29) 937次浏览
配置<mvc:annotation-driven ..>的方式不行,直接配置文件都不通过,启动报错。
<bean class=”org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter”>的方式依然启动报错。
不知道什么版本开始,spring的消息消息转换器的配置方式和网上提供的都不一样了?
麻烦哪位高手给个在SpringMVC 4.2.X版本以上测试通过的配置fastjson的粒子~~ 就是使用@ResponseBody时使用FastJson
解决方案

40

<!-- 会自动注册了validator  ConversionService  -->
    <mvc:annotation-driven>
        <!--enableMatrixVariables="true">-->
        <mvc:message-converters register-defaults="true">
            <!-- StringHttpMessageConverter编码为UTF-8,防止乱码 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8"/>
                <property name="supportedMediaTypes">
                    <list>
                        <bean class="org.springframework.http.MediaType">
                            <constructor-arg index="0" value="text"/>
                            <constructor-arg index="1" value="plain"/>
                            <constructor-arg index="2" value="UTF-8"/>
                        </bean>
                        <bean class="org.springframework.http.MediaType">
                            <constructor-arg index="0" value="*"/>
                            <constructor-arg index="1" value="*"/>
                            <constructor-arg index="2" value="UTF-8"/>
                        </bean>
                    </list>
                </property>
            </bean>
            <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"
                  id="fastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
                <!--<property name="serializerFeature">-->
                <!--这个地方加上这个功能吧,能本人配置一些东西,例如时间的格式化,null输出""等等-->
                <!--</property>-->
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

上面这段,你配置在spring-mvc.xml里面了么

30

参考这个。
http://www.cnblogs.com/rufus-hua/p/5088528.html
这个1.2.7

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明SpringMVC 4.2.4怎么样整合Fastjson?网上找的配置都不行
喜欢 (0)
[1034331897@qq.com]
分享 (0)