首页 > 代码库 > spring mvc3.2.4生成的json配置

spring mvc3.2.4生成的json配置

加入jackson包,在controller使用@ResponseBody注解,OK!

如果要全局支持jsonp(支持jsonp的做法:可以在controller的方法返回String类型,接收一下callback,然后 callback调用一下json结果就可以),可以再加一个StringHttpMessageConverter,不仅能解决中文乱码,还能把 json里面的换行\r\n去掉.


<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />  
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">  
    <property name="messageConverters">  
        <list>  
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">  
                <property name="supportedMediaTypes">  
                    <list>  
                        <value>text/html; charset=UTF-8</value>  
                        <value>application/json;charset=UTF-8</value>  
                    </list>  
                </property>  
            </bean>  
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
                <property name="supportedMediaTypes">  
                    <list>  
                        <value>text/html; charset=UTF-8</value>  
                        <value>application/json;charset=UTF-8</value>  
                    </list>  
                </property>  
            </bean>  
        </list>  
    </property>  
</bean>