springRest+cross跨域支持
2016-06-17 15:51
471 查看
最近一个接口项目需要支持跨域访问,基于cross实现。于是去spring官网上找了下,发现spring版本4.2+开始支持通过@CrossOrigin注解实现跨域支持。于是把spring版本升级到4.2.6,下面记录踩过的坑。
controller只需要在原先方法上面添加@CrossOrigin注解即可
主要的坑出现在配置文件上,原来的org.springframework.web.servlet.view.ContentNegotiatingViewResolver这个类重写了,导致原来的配置不能使用,新的配置如下
到此可以通过jsonp来跨越请求了,算是个小小的坑吧,记录一下。
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来跨越请求了,算是个小小的坑吧,记录一下。
相关文章推荐
- 【第八章】 对ORM的支持 之 8.4 集成JPA ——跟我学spring3
- Java 9 和Spring Boot 2.0纷纷宣布支持的HTTP/2到底是什么?
- SpringMvc支持跨域访问,Spring跨域访问,SpringMvc @CrossOrigin 跨域
- SpringBoot对消息队列(MQ)的支持
- fetch跨域请求 + spring mv 跨域支持
- Spring+NamedParameterJdbcTemplate+NamedParameterJdbcDaoSupport+JDBC 命名参数支持
- 【spring boot】2.0增加跨域请求支持 全局配置 以及局部配置
- Spring4 对Bean Validation规范的新支持(方法级别验证)
- xml方式实现spring的声明式事务管理及对jdbc操作的支持
- 【第八章】 对ORM的支持 之 8.1 概述 8.2 集成Hibernate——跟我学spring3
- Spring-----14、Spring的“零配置”支持
- SpringMVC+Freemarker+JSTL支持
- 不明觉厉的spring(4)---spring对持久层(jdbc,hibernate)以及事务的支持
- Spring-jdbc:使用支持具名参数的JdbcTemplate
- 被忽略的Spring3小改进—支持多数据源的@Transactional事务注解
- Spring攻略学习笔记(3.01)------在Spring中启用AspectJ注解支持
- 不明觉厉的spring(7)---spring对远程服务的支持(对rmi和jms支持)
- spring中增加自定义配置支持
- Spring 3支持RESTful API/APP配置示例
- 开涛spring3(7.4) - 对JDBC的支持 之 7.4 Spring提供的其它帮助