在学习ajax异步请求的时候如果通过post请求发送json格式的请求参数服务端是无法通过request.getParameter()方法获取请求参数的。那么在ssm学习过程中我学到了一个处理json格式的请求参数的注解RequestBody。它的作用是将请求体中的内容和我们后端控制器方法的形参进行绑定获取到字符串类型的请求数据。让我们先来看前端代码:这里我使用了axios和vue。所以在实现相关的代码之前我们需要引入axios.min.js和vue.js。!DOCTYPE html html langen head meta charsetUTF-8 title首页/title /head body div idapp input typebutton value使用RequestBody注解处理json格式的请求参数 clicktestRequestBody/input /div script typetext/javascript th:src{/static/axios.min.js}/script script typetext/javascript th:src{/static/vue.js}/script script typetext/javascript let vue new Vue({ el:#app, methods:{ testRequestBody(){ axios.post( test/RequestBody/json, {username:admin, password:123456,age:18,gender:男} ).then(response { console.log(response.data) }).catch(error { console.log(error.message) }) } } }) /script /body /html在vue对象中我们重点关注methods属性的第二个方法testRequestBody()。这里我们向服务器发送了一个json格式的请求数据。{username:admin, password:123456,age:18,gender:男}在后端服务器中我们设置了对应url的控制器方法。RequestMapping(/test/RequestBody/json) public void testRequestBody(HttpServletResponse response, RequestBody String requestBody) throws IOException { System.out.println(requestBody:requestBody); response.getWriter().write(Hello,AjaxAxios); } public void testRequestBody(RequestBody MapString,Object map, HttpServletResponse response) throws IOException { System.out.println(map); response.getWriter().write(Hello,RequestBody); }public void testRequestBody(RequestBody User user, HttpServletResponse response) throws IOException { System.out.println(user); response.getWriter().write(Hello,RequestBody); }在讲解方法代码之前我得先说明几点注意事项:1.ajax异步请求旨在仅在修改局部页面来实现对于用户操作的响应。因此我们这里虽然设置了Thymeleaf的ThymeleafViewResolver来简化页面跳转操作但是我们不需要这样做。因为我们只是想在当前页面获取响应信息。所以这里的返回值不是String而是void。2.在使用axios时我可以使用两种方式来发送异步请求。第一种方式:axios({url:test/RequestBody/jsonmethod:post// params:{}data:{username:admin, password:123456,age:18,gender:男}}) .then(response {console.log(response.data)}) .catch(error {console.log(error.message)})第二种方式:axios.post(test/RequestBody/json{username:admin, password:123456,age:18,gender:男}) .then(response {console.log(response.data)}) .catch(error {console.log(error.message)})这里我想说明两点:1.对于第一种方法params和data属性对应的值都是请求参数。两者的不同之处在于params属性中的参数无论是请求方式是get还是post请求的参数以keyvalue形式自动拼接到url后面这种方式可以在后端通过request.getParameter()获取。而data属性的参数是以json格式保存到请求报文中的请求体了这里的请求方式不能为get。因此是不能通过getParameter()方法获取的。2.url如果在配置tomcat服务器时设置了访问的上下文路径的话就必须加上。这里我设置上下文路径为“/”因此省略。接下来我们来解析后端代码对于第一个方法。我们只能获取到字符串类型的响应数据。但是很显然这不是我们想获取到的数据。我们想获取到的是给个key然后就获取到对应value的类似map集合的数据。public void testRequestBody(HttpServletResponse response, RequestBody String requestBody) throws IOException { System.out.println(requestBody:requestBody); response.getWriter().write(Hello,AjaxAxios); }所以我们就引入了后面两个方法。但要将json格式的数据进行解析有三个要求:1.导入jackson的依赖包dependency groupIdcom.fasterxml.jackson.core/groupId artifactIdjackson-databind/artifactId version2.12.1/version /dependency2.在springmvc的配置文件中开启注解驱动mvc:annotation-driven/3.处理请求的控制器方法的形参位置直接设置json格式的请求参数要转换的java类型的形参 使用RequestBody注解表示即可。如下面两个方法中的map和user。public void testRequestBody(RequestBody MapString,Object map, HttpServletResponse response) throws IOException { System.out.println(map); response.getWriter().write(Hello,RequestBody); }public void testRequestBody(RequestBody User user, HttpServletResponse response) throws IOException { System.out.println(user); response.getWriter().write(Hello,RequestBody); }如果在后端中没有对应的java类型我们就可以使用map集合以key-value形式获取响应数据。如果后端有对应的java类型我们就可以直接使用对应的类型自动生成对应类的实例。