目录
⭐使用@RequestBody注解将json格式的请求参数转换为Java对象
2.在SpringMVC的配置文件中设置开启MVC的注解驱动
3.在处理请求的控制器方法的形参位置,直接设置json格式的请求参数要转换的Java类型的形参
2.在SpringMVC的配置文件中设置开启MVC的注解驱动
配置web.xml
- "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
- http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.2.xsd">
-
-
- <context:component-scan base-package="com.atguigu.controller">context:component-scan>
-
- <bean id="viewResolver"
- class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
- <property name="order" value="1"/>
- <property name="characterEncoding" value="UTF-8"/>
- <property name="templateEngine">
- <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
- <property name="templateResolver">
- <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
-
-
-
-
- <property name="prefix" value="/WEB-INF/templates/"/>
-
- <property name="suffix" value=".html"/>
- <property name="templateMode" value="HTML5"/>
- <property name="characterEncoding" value="UTF-8" />
- bean>
- property>
- bean>
- property>
- bean>
-
-
- <mvc:default-servlet-handler/>
-
-
- <mvc:annotation-driven/>
-
-
- <mvc:view-controller path="/" view-name="index">mvc:view-controller>
- beans>
创建控制层
- package com.atguigu.controller;
-
- import org.springframework.stereotype.Controller;
-
- @Controller
- public class TestAjaxController {
- }
创建首页
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>首页title>
- head>
- <body>
-
- <div id="app">
- <h1>index.htmlh1>
- <input type="button" value="测试SpringMVC处理ajax请求" @click="testAjax()">
- div>
-
- <script type="text/javascript" th:src="@{/js/vue.js}">script>
- <script type="text/javascript" th:src="@{/js/axios.miin.js}">script>
- <script type="text/javascript">
- /**
- * axios({
- url:"", 请求路径
- method:"", 请求方式
- params:{},
- 以 name=value&name=value方式发送的请求参数
- 不管使用的请求方式是get还是post,请求参数都会被拼接到请求地址后
- 此种方式的请求参数可以通过request.getParameter()获取
- data:{}
- 以json格式发送的请求参数
- 请求参数会被保存到请求报文的请求体传输到服务器(get没有请求体)
- 此种方式的请求参数不可以通过request.getParameter()获取
- }).then(response=>{
- console.log(response.data);
- });
- */
- var vue = new Vue({
- el:"#app",
- methods:{
- testAjax(){
- axios.post("");
- }
- }
- });
- script>
- body>
- html>
项目目录

axios中文文档|axios中文网 | axios (axios-js.com)

- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>首页title>
- head>
- <body>
- <div id="app">
- <h1>index.htmlh1>
- <input type="button" value="测试SpringMVC处理ajax请求" @click="testAjax()">
- div>
- <script type="text/javascript" th:src="@{/js/vue.js}">script>
- <script type="text/javascript" th:src="@{/js/axios.min.js}">script>
- <script type="text/javascript">
- var vue = new Vue({
- el:"#app",
- methods:{
- testAjax(){
- axios.post(
- "/SpringMVC/test/ajax?id=1001",
- {username:"admin",password:"123456"}
- ).then(response=>{
- console.log(response.data)
- });
- }
- }
- });
- script>
- body>
- html>
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
- @Controller
- public class TestAjaxController {
- @RequestMapping("/test/ajax")
- public void testAjax(Integer id, HttpServletResponse response) throws IOException {
- System.out.println("id :" + id);
- response.getWriter().write("hello,axios");
- }
- }



将请求体中的内容和控制器方法的形参进行绑定
@RequestBody可以获取请求体信息,使用@RequestBody注解标识控制器方法的形参,当前请求的请求体就会为当前注解所标识的形参赋值
- @RequestMapping("/test/ajax")
- public void testAjax(Integer id, @RequestBody String requestBody, HttpServletResponse response) throws IOException {
- System.out.println("requestBody :" + requestBody);
- System.out.println("id :" + id);
- response.getWriter().write("hello,axios");
- }

在使用了axios发送ajax请求之后,浏览器发送到服务器的请求参数有两种格式:
(在pom.xml中)
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-databindartifactId>
- <version>2.11.3version>
- dependency>
<mvc:annotation-driven/>
使用 @RequestBody注解标识即可
⚪创建实体类User
- package com.atguigu.pojo;
-
- public class User {
- private Integer id;
- private String username;
- private String password;
- private Integer age;
- private String gender;
-
- public User() {
- }
-
- public User(Integer id, String username, String password, Integer age, String gender) {
- this.id = id;
- this.username = username;
- this.password = password;
- this.age = age;
- this.gender = gender;
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getUsername() {
- return username;
- }
-
- public void setUsername(String username) {
- this.username = username;
- }
-
- public String getPassword() {
- return password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
-
- public String getGender() {
- return gender;
- }
-
- public void setGender(String gender) {
- this.gender = gender;
- }
-
- @Override
- public String toString() {
- return "User{" +
- "id=" + id +
- ", username='" + username + '\'' +
- ", password='" + password + '\'' +
- ", age=" + age +
- ", gender='" + gender + '\'' +
- '}';
- }
- }
⚪控制器方法
- @RequestMapping("/test/RequestBody/json")
- public void testRequestBody(@RequestBody User user,HttpServletResponse response) throws IOException {
- System.out.println(user);
- response.getWriter().write("hello.RequestBody");
- }
- @RequestMapping("/test/RequestBody/json")
- public void testRequestBody(@RequestBody Map
map, HttpServletResponse response) throws IOException { - System.out.println(map);
- response.getWriter().write("hello.RequestBody");
- }
@ResponseBody:将所标识的控制器方法的返回值作为响应报文的响应体响应到浏览器
<a th:href="@{/test/ResponseBody}">测试@ResponseBody注解响应浏览器数据a><br>
- @RequestMapping("/test/ResponseBody")
- @ResponseBody
- public String testResponseBody(){
- return "success";
- }

(在pom.xml中)
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-databindartifactId>
- <version>2.11.3version>
- dependency>
<mvc:annotation-driven/>
<input type="button" value="使用@ResponseBody注解响应json格式的数据" @click="testResponseBody()">
- testResponseBody(){
- axios.post(
- "/SpringMVC/test/ResponseBody/json"
- ).then(response=>{
- console.log(response.data);
- });
- }
响应不同的数据:
- public User testResponseBodyJson(){
- User user = new User(1001, "admin", "123456", 20, "男");
- return user;
- }
- @RequestMapping("/test/ResponseBody/json")
- @ResponseBody
- public List
testResponseBodyJson(){ - User user1 = new User(1001, "admin1", "123456", 20, "男");
- User user2 = new User(1002, "admin2", "123456", 20, "男");
- User user3 = new User(1003, "admin3", "123456", 20, "男");
- List
list = Arrays.asList(user1, user2, user3); - return list;
- }
- public Map
testResponseBodyJson(){ - User user1 = new User(1001, "admin1", "123456", 20, "男");
- User user2 = new User(1002, "admin2", "123456", 20, "男");
- User user3 = new User(1003, "admin3", "123456", 20, "男");
- Map
map = new HashMap<>(); - map.put("1001", user1);
- map.put("1002", user2);
- map.put("1003", user3);
- return map;
- }
@RestController注解是springMVC提供的一个复合注解
标识在控制器的类上
相当于为类添加了 @Controller注解,并且为其中的每个方法添加了@ResponseBody注解