From a62348f97e2c95a4d7fee1d3dd1ec79b9e919819 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=9D=8E=E8=88=92=E6=B1=B6?= <3228916049@qq.com>
Date: Thu, 4 Jan 2024 14:08:24 +0000
Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: 李舒汶 <3228916049@qq.com>
---
.../20240104REST\347\254\224\350\256\260.md" | 325 ++++++++++++++++++
1 file changed, 325 insertions(+)
create mode 100644 "49 \346\235\216\350\210\222\346\261\266/20240104REST\347\254\224\350\256\260.md"
diff --git "a/49 \346\235\216\350\210\222\346\261\266/20240104REST\347\254\224\350\256\260.md" "b/49 \346\235\216\350\210\222\346\261\266/20240104REST\347\254\224\350\256\260.md"
new file mode 100644
index 0000000..a37c1dd
--- /dev/null
+++ "b/49 \346\235\216\350\210\222\346\261\266/20240104REST\347\254\224\350\256\260.md"
@@ -0,0 +1,325 @@
+## 笔记
+
+REST简介
+
+REST(Representational State Transfer)
+
+优点:
+
+隐藏资源的访问行为,无法通过地址得知对资源是何种操作
+
+书写简化
+
+名称:@RequestMapping
+
+类型:方法注解
+
+位置:SpringMVC控制器方法定义上方
+
+作用:设置当前控制器方法请求访问路径
+
+属性
+
+value(默认):请求访问路径
+
+method:http请求动作,标准动作(GET/POST/PUT/DELETE)
+
+名称:@PathVariable
+
+类型:形参注解
+
+位置:SpringMVC控制器方法形参定义前面
+
+作用:绑定路径参数与处理器方法形参间的关系,要求路径参数名与形参名一一对应
+
+@RequestBody @RequestParam @PathVariable
+
+区别
+
+@RequestParam用于接收url地址传参或表单传参
+
+@RequestBody用于接收json数据
+
+@PathVariable用于接收路径参数,使用{参数名称}描述路径参数
+
+应用
+
+后期开发中,发送请求参数超过1个时,以json格式为主,@RequestBody应用较广
+
+如果发送非json格式数据,选用@RequestParam接收请求参数采用RESTful进行开发,当参数数量较少时,例如1个,
+
+可以采用@PathVariable接收请求路径变量,通常用于传递id值
+
+## 作业
+
+pom.xml
+
+```xml
+
+ 4.0.0
+ com.xlu
+ ssmvc03
+ war
+ 1.0-SNAPSHOT
+
+
+
+ org.springframework
+ spring-webmvc
+ 5.2.25.RELEASE
+
+
+ javax.servlet
+ javax.servlet-api
+ 3.1.0
+
+ provided
+
+
+
+
+ com.fasterxml.jackson.core
+ jackson-databind
+ 2.11.3
+
+
+
+
+
+
+
+ org.apache.tomcat.maven
+ tomcat7-maven-plugin
+ 2.2
+
+ 80
+ /
+
+ utf-8
+
+
+
+
+
+
+
+```
+
+src/main/java/com/xlu/controller/UserController.java
+
+```java
+package com.xlu.controller;
+
+import com.xlu.admin.User;
+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;
+
+import java.util.Arrays;
+import java.util.List;
+
+@Controller
+public class UserController {
+
+ //接受用户名和慢慢,登录
+ @RequestMapping("/login")
+ @ResponseBody
+ public String login(User user){
+
+ String result = "login error";
+ if ("admin".equals(user.getUsername())&&"123456".equals(user.getPassword())) {
+ result = "login success";
+ }
+ return result;//输入用户名admin,密码123456,显示结果巍峨login success
+ }
+
+ //模拟注册
+ @RequestMapping(value = "/reg",produces = "text/html;charset=utf-8")//解决返回的中文乱码问题
+ @ResponseBody
+ public String reg(User user){
+
+ System.out.println(user);
+
+ return "注册成功!";
+ }
+
+ //json集合
+ @RequestMapping("/jsonArray")
+ @ResponseBody
+ public String jsonData(@RequestBody List names){
+
+ System.out.println(names);
+
+ return "ok";
+ }
+
+// JSON数组
+ @RequestMapping("/jsonArray2")
+ @ResponseBody
+ public String jsonData2(@RequestBody String[] names){
+
+ System.out.println(Arrays.toString(names));
+
+ return "ok";
+ }
+
+// json对象
+ @RequestMapping("/userJson")
+ @ResponseBody
+ public String jsonUser(@RequestBody User user){
+ System.out.println(user);
+ return user.toString();
+ }
+
+ @RequestMapping("/userJson2")
+ @ResponseBody
+ public String jsonUser2(@RequestBody List user){
+ System.out.println(user);
+ return user.toString();
+ }
+
+
+}
+
+```
+
+src/main/java/com/xlu/config/WebConfig.java
+
+```java
+package com.xlu.config;
+
+import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
+
+public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
+ protected Class>[] getRootConfigClasses() {
+ return new Class[0];
+ }
+
+ protected Class>[] getServletConfigClasses() {
+ return new Class[]{SpringMvcConfig.class};
+ }
+
+ protected String[] getServletMappings() {
+ return new String[]{"/"};
+ }
+}
+
+```
+
+src/main/java/com/xlu/config/SpringMvcConfig.java
+
+```java
+package com.xlu.config;
+
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
+
+@Configuration
+@ComponentScan("com.xlu.controller")
+@EnableWebMvc
+public class SpringMvcConfig {
+}
+
+```
+
+src/main/java/com/xlu/admin/User.java
+
+```java
+package com.xlu.admin;
+
+public class User {
+
+ private Integer id;
+ private String username;
+ private String password;
+ private Address address;
+
+ @Override
+ public String toString() {
+ return "User{" +
+ "id=" + id +
+ ", username='" + username + '\'' +
+ ", password='" + password + '\'' +
+ ", address=" + address +
+ '}';
+ }
+
+ 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 Address getAddress() {
+ return address;
+ }
+
+ public void setAddress(Address address) {
+ this.address = address;
+ }
+
+ public User(Integer id, String username, String password, Address address) {
+ this.id = id;
+ this.username = username;
+ this.password = password;
+ this.address = address;
+ }
+
+ public User() {
+ }
+}
+
+```
+
+src/main/java/com/xlu/admin/Address.java
+
+```java
+package com.xlu.admin;
+
+public class Address {
+ private String city;
+
+ @Override
+ public String toString() {
+ return "Address{" +
+ "city='" + city + '\'' +
+ '}';
+ }
+
+ public String getCity() {
+ return city;
+ }
+
+ public void setCity(String city) {
+ this.city = city;
+ }
+
+ public Address(String city) {
+ this.city = city;
+ }
+
+ public Address() {
+ }
+}
+```
--
Gitee