spring MVC 配置支持JSONP
来源:互联网 发布:linux cp mv 速度 编辑:程序博客网 时间:2024/05/21 10:08
配置文件
<?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>
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(); }}
2 0
- spring MVC 配置支持JSONP
- Spring MVC 4.1 支持jsonp
- Spring MVC 4.1 支持jsonp
- Spring MVC 4+ JsonP支持
- Spring MVC 4.1 使用ResponseBodyAdvice支持jsonp
- spring mvc 对jsonp的支持
- spring mvc ajax jsonp 跨域请求 返回值 配置
- JSONP跨域,及Spring MVC返回JSONP数据
- spring mvc 环境Fastjson转换器jsonp请求
- spring MVC配置form支持PUT和DELETE方法
- 【spring mvc】spring-mvc配置
- spring mvc @Responsebody支持问题
- spring mvc 支持options方法
- spring mvc支持返回json
- spring mvc 支持options方法
- Ajax+Spring MVC实现跨域请求(JSONP)
- Ajax+Spring MVC实现跨域请求(JSONP)
- Spring MVC jsonp跨域调用 中文乱码问题处理
- 信息学习18
- VS2008资源视图问题
- shading 函数
- matlab小波分析工具箱常用函数
- DBUtils使用详解
- spring MVC 配置支持JSONP
- 功率谱密度相关方法的MATLAB…
- VC++学习之GDI概述
- 【MATLAB】matlab eig 函数…
- ContinuousSum_n
- matlab eval命令
- matlab线性拟合
- 《Python核心编程》第13章 面向对象编程 练习
- Matlab概率统计工具箱--假设检验