配置文件
<?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();
}
}