json数据交互
就是将前端传的json数据转为java对象,也可以将对象转换json 返回给前端
创建一个maven项目
要是web项目哦!最重要的不要忘记添加tomcat哦!
1.在pom.xml中引入依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spirng.version>5.0.16.RELEASE</spirng.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 添加 servlet 依赖
<scope>provided</scope> 编译时需要,运行时不需要
-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spirng.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spirng.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.12</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.12</version>
</dependency>
<!-- 引入spring mvc jar-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spirng.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.71</version>
</dependency>
<!--spring mvc-json依赖-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.9</version>
</dependency>
</dependencies>
2.修改webapp\WEB-INF\web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.开启json自动转化
在resources下创建一个:spring-servlet.xml
写入以下代码
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--
开启全局扫描注解
-->
<context:component-scan base-package="com"></context:component-scan>
<!--
开启json自动转化
-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置静态资源
DispatcherServlet 就不去处理 mapping 下路径匹配的资源,交给tomcat处理
mapping="/html/*" 匹配的路径 凡是遇到 访问 http://localhost:8080/html/ajax.html 就去
location="/html/"
-->
<mvc:resources mapping="/html/*" location="/html/"></mvc:resources>
<!-- 告诉DispatcherServlet 不需要处理/js/ 这些资源,让tomcat 处理 -->
<mvc:resources mapping="/js/*" location="/js/"></mvc:resources>
</beans>
4.在webapp下创建一个html文件夹和一个js文件夹,在html文件夹中创建一个ajax.html,在js文件中引入一个jquery包
5.在ajax.html中编写
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="/js/jquery-3.3.1.js"></script>
</head>
<body>
<script>
function updateStudent() {
$.ajax({
url:"/updateStudent",
data:JSON.stringify({id:$('#id').val(),name:$('#name').val(),birthday:$('#birthday').val(),age:$('#age').val(),sex:$('#sex').val()}),
type:'post',
contentType:"application/json;charset=UTF-8",
dataType:'json',
success:function (data) {
alert(JSON.stringify(data))
},
error:function () {
alert("服务器错误")
}
})
}
</script>
学生id:<input name="id" id="id" type="number"><br>
学生姓名:<input id="name" type="text" name="name"><br>
学生生日:<input id="birthday" type="text" name="birthday"><br>
学生年龄:<input id="age" type="text" name="age"><br>
学生性别:<input id="sex" type="text" name="sex"><br>
<button type="button" onclick="updateStudent()">更新学生</button>
</body>
</html>
6.在java下创建以下项目
7.在student类中写入以下代码
package com.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable;
import java.util.Date;
public class Student implements Serializable {
private int id;
// @JsonProperty("student_name")
// 前端传递 student_name json数据 接收 java为name属性 设置
// java ---json 将name ---> student_name
private String name;
// Date 返回前端 转换为 YYYY-MM-DD HH:mm:ss
//* 1.可以将j'son 中的字符串 yyyy-MM-dd HH:mm:ss 转换为 date
// * 2. 返回json 数据时 可以将 data 转为yyyy-MM-dd HH:mm:ss 的json
// timezone = "GMT+8" 时区问题 需要在json 得到的结果 + 8小时
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date birthday;
@JsonInclude(JsonInclude.Include.NON_EMPTY) // 对 int 不生效
private int age;
// @JsonInclude(JsonInclude.Include.NON_EMPTY) //转换json数据时 sex 不为""时才返回
@JsonInclude(JsonInclude.Include.NON_NULL)//转换json数据时 sex 不为null时才返回
private String sex;
public Student(int id, String name, Date birthday, int age, String sex) {
this.id = id;
this.name = name;
this.birthday = birthday;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", birthday=" + birthday +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
public Student() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
8.在StudentController中写入以下代码
package com.controller;
import com.entity.Student;
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
public class StudentController {
@RequestMapping("/updateStudent")
@ResponseBody// 返回json 数据
// @RequestBody Student student 将接收到的 json 数据转化为 Student 对象
public Student updateStudent(@RequestBody Student student){
System.out.println("更新学生"+student);
return student;
}
}
9.不要忘记添加tomcat,就可以启动了!
访问 http://localhost:8080/html/ajax.html
界面有点丑