springMVC异步返回json对象
1、释放静态资源
在springMVC文件中配置释放静态资源
<!--释放静态资源-->
<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
2、配置将对象解析为json格式字符串的依赖
<!--jackson相关依赖坐标-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
3、前端发送异步请求
package com.xsl.controller;
import com.xsl.entity.UserEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/testJson")
//在返回值前加@ResponseBody注解,直接返回对象即可
public @ResponseBody UserEntity testJson(@RequestBody UserEntity user){
return user;
}
}