controller只需要在原先方法上面添加@CrossOrigin注解即可
@Controller @RequestMapping("demo") public class DemoController { @CrossOrigin @RequestMapping("test") public void test(ModelMap map ){ map.put("say", "hello world!"); }; }
主要的坑出现在配置文件上,原来的org.springframework.web.servlet.view.ContentNegotiatingViewResolver这个类重写了,导致原来的配置不能使用,新的配置如下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "> <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/> <mvc:resources location="/resources/" mapping="/resources/**" /> <context:component-scan base-package="com.chenxun.demo.*" /> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <!-- 开启对/blog/123.json的支持--> <property name="favorPathExtension" value="true" /> <!-- 关闭 /blog/123?format=json 的支持 --> <property name="favorParameter" value="false" /> <!-- 自定义约定类型参数名,默认为format --> <property name="parameterName" value="mediaType" /> <!-- 忽略AcceptHeader --> <property name="ignoreAcceptHeader" value="true"/> <!-- 关闭MIME类型解析 --> <property name="useJaf" value="false"/> <property name="defaultContentType" value="application/json" /> <property name="mediaTypes"> <map> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> <entry key="html" value="text/html" /> </map> </property> </bean> <!-- 根据客户端的不同的请求决定不同的view进行响应, 如 /blog/1.json /blog/1.xml --> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="order" value="1" /> <property name="contentNegotiationManager" ref="contentNegotiationManager"></property> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> </list> </property> <property name="defaultViews"> <list> <!-- for application/json --> <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"> <property name="objectMapper"> <bean class="com.fasterxml.jackson.databind.ObjectMapper"> <property name="dateFormat"> <bean class="java.text.SimpleDateFormat"> <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" /> </bean> </property> </bean> </property> </bean> <!-- for application/xml --> <bean class="org.springframework.web.servlet.view.xml.MarshallingView"> <property name="marshaller"> <bean class="org.springframework.oxm.xstream.XStreamMarshaller"></bean> </property> </bean> </list> </property> </bean> </beans>
到此可以通过jsonp来跨越请求了,算是个小小的坑吧,记录一下。