spring MVC 配置支持JSONP

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <!-- 扫描的包名 -->
    <context:component-scan base-package="com.gochinatv.api.controller.tv"></context:component-scan>
    <context:component-scan base-package="com.gochinatv.vrs.framework.service.cacheService"/>
    <!-- 默认的注解映射支持 -->
    <mvc:annotation-driven/>
    <context:annotation-config/>


    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="order" value="1"/>
        <property name="favorPathExtension" value="true"/>
        <property name="mediaTypes">
            <map>
                <entry key="json" value="application/json"/>
                <entry key="jsonp" value="application/javascript"/>
                <!-- <entry key="xml" value="application/xml" /> -->
            </map>
        </property>
        <!-- 设置为true以忽略对Accept Header的支持-->
        <property name="defaultContentType" value="application/json"></property>
        <property name="defaultViews">
            <list>
                <bean class="com.gochinatv.api.util.CustomMappingJacksonJsonView">
                    <property name="disableCaching" value="false"/>
                </bean>
                <bean class="com.gochinatv.api.util.CustomMappingJacksonJsonpView"/>
            </list>
        </property>
    </bean>
</beans>


CustomMappingJacksonJsonpView


package com.gochinatv.api.util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * jsonp
 */
public class CustomMappingJacksonJsonpView extends CustomMappingJacksonJsonView {
    /**
     * Default content type. Overridable as bean property.
     */
    public static final String DEFAULT_CONTENT_TYPE = "application/javascript";

    @Override
    public String getContentType() {
        return DEFAULT_CONTENT_TYPE;
    }

    @Override
    public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        if ("GET".equals(request.getMethod().toUpperCase())) {
            @SuppressWarnings("unchecked")
            Map<String, String[]> params = request.getParameterMap();

            if (params.containsKey("callback")) {
                response.getOutputStream().write(new String(params.get("callback")[0] + "(").getBytes());
                super.render(model, request, response);
                response.getOutputStream().write(new String(");").getBytes());
                response.setContentType(DEFAULT_CONTENT_TYPE);
            } else {
                super.render(model, request, response);
            }
        } else {
            super.render(model, request, response);
        }
    }


}

CustomMappingJacksonJsonView


package com.gochinatv.api.util;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * BUG:MappingJacksonJsonView返回 {model类名:{内容}}
 */
public class CustomMappingJacksonJsonView extends MappingJacksonJsonView {

    @Override
    protected Object filterModel(Map<String, Object> model) {
        Map<?, ?> result = (Map<?, ?>) super.filterModel(model);
        if (result.size() == 1) {
            return result.values().iterator().next();
        } else {
            return result;
        }
    }

    protected void renderMergedOutputModel(Map<String, Object> model,
                                           HttpServletRequest request, HttpServletResponse response) throws Exception {
        Object value = filterModel(model);
        response.setContentType("text/plain; charset=GBK");
        response.getOutputStream().write(value.toString().getBytes());
        response.getOutputStream().flush();
    }
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值