16.SpringMVC Restful

 

项目目录结构:

项目依赖包:

Vo: UserVo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.course.vo;

import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;

public class UserVo {

private Integer id;
private String name;
private Integer age;
private String address;

// 前端->后端: 时间类型格式转换
@DateTimeFormat(pattern = "yyyy-MM-dd")
// 后端->前端: 时间类型格式转换,使用Jackson生成json对象时把时间进行格式化
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date time;

public UserVo() {
super();
}

public UserVo(Integer id, String name, Integer age, String address, Date time) {
super();
this.id = id;
this.name = name;
this.age = age;
this.address = address;
this.time = time;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public Date getTime() {
return time;
}

public void setTime(Date time) {
this.time = time;
}

@Override
public String toString() {
return "UserVo [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + ", time=" + time + "]";
}

}

Controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.course.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.course.vo.UserVo;

@Controller
@RequestMapping("userModule")
public class UserController {

@RequestMapping(value = "user", method = RequestMethod.POST)
@ResponseBody
public String addUser(UserVo userVo) {
System.out.println("addUser: ");
System.out.println(userVo);
// addUser:
// UserVo [id=1, name=user1, age=10, address=address1, time=Sun May 01 00:00:00 CST 2022]
return "success: addUser, userVo = " + userVo;
}

@RequestMapping(value = "user", method = RequestMethod.PUT)
@ResponseBody
public String updateUser() {
System.out.println("updateUser: ");
// updateUser:
return "success: updateUser";
}

@RequestMapping(value = "user/{id}", method = RequestMethod.DELETE)
@ResponseBody
public String deleteUserById(@PathVariable(value = "id") Integer id) {
System.out.println("deleteUserById: ");
System.out.println("id = " + id);
// deleteUserById:
// id = 1
return "success: deleteUserById, id = " + id;
}

@RequestMapping(value = "user/{id}", method = RequestMethod.GET)
@ResponseBody
public String getUserById(@PathVariable(value = "id") Integer id) {
System.out.println("getUserById: ");
System.out.println("id = " + id);
// getUserById:
// id = 2
return "success: getUserById, id = " + id;
}

@RequestMapping(value = "user", method = RequestMethod.PATCH)
@ResponseBody
public List<UserVo> getAllUser() {
UserVo userVo1 = new UserVo(1, "user1", 10, "address1", new Date());
UserVo userVo2 = new UserVo(2, "user2", 11, "address2", new Date());
UserVo userVo3 = new UserVo(3, "user3", 11, "address3", new Date());
List<UserVo> userVoList = new ArrayList<>();
userVoList.add(userVo1);
userVoList.add(userVo2);
userVoList.add(userVo3);

System.out.println("getAllUser: userVoList = " + userVoList);
// getAllUser: userVoList = [UserVo [id=1, name=user1, age=10, address=address1, time=Sun May 15 11:16:24 CST 2022], UserVo [id=2, name=user2, age=11, address=address2, time=Sun May 15 11:16:24 CST 2022], UserVo [id=3, name=user3, age=11, address=address3, time=Sun May 15 11:16:24 CST 2022]]

return userVoList;
}

}

log4j.properties

1
2
3
4
5
6
7
8
# Global logging configuration
log4j.rootLogger=INFO, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

springmvc.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">


<!--
配置控制器
包扫描
-->
<context:component-scan base-package="com.course.controller"></context:component-scan>

<!--
合并注解的控制器映射器和控制器适配器的配置
对控制器映射器和控制器适配器进行加强,如把对象转化为json字符串
-->
<mvc:annotation-driven></mvc:annotation-driven>

<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置前缀 -->
<property name="prefix" value="/WEB-INF/view/"></property>
<!-- 配置后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>

</beans>

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>01_springmvc_hello</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<!-- 配置SpringMVC编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 配置编码的值 -->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<!--
方法1: <url-pattern>*.do</url-pattern>
<url-pattern>/*</url-pattern>
-->
<!-- 指定某个servlet去过滤 -->
<servlet-name>springmvc</servlet-name>
</filter-mapping>

<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!-- 加载springmvc.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>

<!-- 启动加载 -->
<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- <url-pattern>*.do</url-pattern> -->
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>
<button id="addUserButton" onclick="addUser()">addUser</button>
<button id="updateUserButton" onclick="updateUser()">updateUser</button>
<button id="deleteUserByIdButton" onclick="deleteUserById()">deleteUserById</button>
<button id="getUserByIdButton" onclick="getUserById()">getUserById</button>
<button id="getAllUserButton" onclick="getAllUser()">getAllUser</button>
</p>
</body>
<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
<script type="text/javascript">

function addUser() {
$.ajax({
url: "userModule/user",
type: "post",
data: {
"id": 1,
"name": "user1",
"age": 10,
"address": "address1",
"time": "2022-05-01"
},
success: function(message) {
console.log(message);
}
});
}

function updateUser() {
$.ajax({
url: "userModule/user",
type: "put",
data: {
"id": 1,
"name": "user1",
"age": 10,
"address": "address1",
"time": "2022-05-01"
},
success: function(message) {
console.log(message);
}
});
}

function deleteUserById() {
$.ajax({
url: "userModule/user/1",
type: "delete",
success: function(message) {
console.log(message);
}
});
}

function getUserById() {
$.ajax({
url: "userModule/user/2",
type: "get",
success: function(message) {
console.log(message);
}
});
}

function getAllUser() {
$.ajax({
url: "userModule/user",
type: "patch",
success: function(data) {
console.log(data);
}
});
}
</script>

</html>

打开浏览器,访问: http://localhost:8080/11_springmvc_restful/index.jsp