码迷,mamicode.com
首页 > Web开发 > 详细

直接用HTTP返回JSON到浏览器

时间:2018-03-08 17:30:46      阅读:367      评论:0      收藏:0      [点我收藏+]

标签:xstream   views   bsp   factor   factory   col   content   stream   size   

spring mvc 配置返回json需要两个重要的bean 

ContentNegotiatingViewResolver
MappingJackson2JsonView

具体看代码

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <property name="mediaTypes">
            <value>
                html=text/html
                json=application/json
                xml=application/xml
            </value>
        </property>
        <property name="defaultContentType" value="text/html"/>
    </bean>

    <!-- ContentNegotiatingViewResolver视图解析器,利用他就可以配置多种返回值 -->
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="order" value="0"/>
        <property name="contentNegotiationManager" ref="contentNegotiationManager"/>

        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
                    <property name="prefix" value="/WEB-INF/jsp/"/>
                    <property name="suffix" value=".jsp"/>
                </bean>
            </list>
        </property>

        <property name="defaultViews">
            <list>
                <bean  class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
                    <property  name="extractValueFromSingleKeyModel" value="true" />
                </bean>
                <bean  class="org.springframework.web.servlet.view.xml.MarshallingView">
                    <property name="marshaller">
                        <bean  class="org.springframework.oxm.xstream.XStreamMarshaller" />
                    </property>
                </bean>
            </list>
        </property>
    </bean>

    <bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"/>

 





 

直接用HTTP返回JSON到浏览器

标签:xstream   views   bsp   factor   factory   col   content   stream   size   

原文地址:https://www.cnblogs.com/BBchao/p/8528964.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!