首先奄侠,關(guān)于遷移至spring4.x需要發(fā)生的配置改動
- json依賴包發(fā)生變化迄靠,maven中獲取
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.0</version>
</dependency>
- 配置文件需要修改
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
- 前端傳回數(shù)據(jù)
$.ajax({
url: 'url',
method: 'POST',
dataType: 'JSON',
contentType:'application/json;charset=UTF-8',
data: JSON.stringify(req_data),
success: function () {
}
});
解決跨域問題
- 全局配置方式
spring servletContext配置文件中添加
<!-- API 接口跨域配置 -->
<mvc:cors>
<mvc:mapping path="/**"
allowed-origins="*"
allowed-methods="POST, GET, OPTIONS, DELETE, PUT"
allowed-headers="Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"
allow-credentials="true" />
</mvc:cors>