前言
使用注解
@ResponseBody:该注解用于将 Controller 的方法返回的对象,通过 HttpMessageConverter 接口转换为指定格式的数据如:json,xml 等,通过 Response 响应给客户端
一、引入的依赖
需要在项目中引入jackson的依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</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-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
二、编写请求
用到@RequestBody 到时就会把请求消息体中的数据取出来,springmvc会自动把同名的key封装到我定义的User body
然后用@ResponseBody把返回的实体又解析成json的格式
@Controller
public class HelloServlet {
@RequestMapping(path = "/response/testAjax",method = {RequestMethod.POST})
public @ResponseBody User testAjax(@RequestBody User body){
System.out.println(body);
UserInfo userInfo = new UserInfo();
userInfo.setInfo("infos");
body.setId(1);
body.setUserInfo(userInfo);
return body;
}
三、配置ajax的异步请求
当一点击这个btn按钮时,就会向指定路径发送一段包含json格式数据的请求
<html>
<head>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#btn").click(function(){
$.ajax({
url:"response/testAjax",
contentType:"application/json;charset=UTF-8",
data:'{"username":"文章","password":"32123","address":"估计费劲"}',
dataType:"json",
type:"post",
success:function(data) {
alert(data);
}
});
});
});
</script>
<title>Title</title>
</head>
<body>
<button id="btn">异步请求</button>
</body>
</html>
三、springmvc的配置
主要是如果有引入jquery的库,或者自己的写的css,就要根据自己的路径配置好不需要springmvc拦截的静态资源<mvc:resources
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
<context:component-scan base-package="com.mediacomm"></context:component-scan>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven/>
</beans>
四、测试
控制台的输出
User{username='文章', password='32123', money='null', address='估计费劲', id=0, userInfo=null, date=null}