您的位置:首页 > 编程语言 > Java开发

springRest+cross跨域支持

2016-06-17 15:51 471 查看
最近一个接口项目需要支持跨域访问,基于cross实现。于是去spring官网上找了下,发现spring版本4.2+开始支持通过@CrossOrigin注解实现跨域支持。于是把spring版本升级到4.2.6,下面记录踩过的坑。
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来跨越请求了,算是个小小的坑吧,记录一下。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: