最近要在spring mvc 上实现 jsonp 所以就查找 spring 官方文档 发现 org.springframework.web.servlet.view.ContentNegotiatingViewResolver 这个东西可以实现
但是在我写demo 的时候去出现了问题 google了 半天也是没有什么结果 。。。
可是无意间改动一个方法的返回值 却成功了 。。。。。
没修改之前
修改之后
但是在我写demo 的时候去出现了问题 google了 半天也是没有什么结果 。。。
可是无意间改动一个方法的返回值 却成功了 。。。。。
<?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:p="http://www.springframework.org/schema/p"
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.1.xsd">
<!-- 默认扫描的包路径 -->
<context:component-scan base-package="com.wzl.openapi"></context:component-scan>
<context:annotation-config />
<mvc:annotation-driven />
<!-- 定义JSP视图解析器-->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="viewClass">
<value>org.springframework.web.servlet.view.InternalResourceView
</value>
</property>
</bean>
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="0" />
<property name="favorParameter" value="true" />
<property name="parameterName" value="format" />
<property name="ignoreAcceptHeader" value="true" />
<!-- true,开启扩展名支持,false关闭支持 -->
<property name="favorPathExtension" value="true" />
<property name="defaultContentType" value="application/json"></property>
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="jsonp" value="application/javascript" />
<entry key="jsp" value="text/html"></entry>
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="0"></property>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<bean class="com.wzl.openapi.util.MappingJacksonJsonpView"></bean>
</list>
</property>
</bean>
</beans>
没修改之前
/**
* 获取验证码
* @param mobile
* @return
*/
@RequestMapping( value = "getCode", produces = "application/json;charset=UTF-8" )
public @ResponseBody Map<String, Object> getCode( Model model, String userMobile ) {
Map<String, Object> map = new HashMap<String, Object>();
try{
Map<String, String> _m = bpwm.getCode( userMobile );
map.put("request", "200");
map.put("data", _m);
}catch (Exception e) {
// TODO: handle exception
map.put("request", e.getMessage());
map.put("data", propes.getPropes().get( "getCode.error." + e.getMessage() ));
}
return map;
}
修改之后
/**
* 获取验证码
* @param mobile
* @return
*/
@RequestMapping( value = "getCode" )
public String getCode( Model model, String userMobile ) {
try{
Map<String, String> _m = bpwm.getCode( userMobile );
model.addAttribute("request", "200");
model.addAttribute("data", _m);
}catch (Exception e) {
// TODO: handle exception
model.addAttribute("request", e.getMessage());
model.addAttribute("data", propes.getPropes().get( "getCode.error." + e.getMessage() ));
}
return "servicesAPI";
}