From b5faf1108ec4b4438496463296a6a402255f2ae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=AE=8F=E8=BE=BE?= <2657224306@qq.com> Date: Thu, 4 Jan 2024 13:18:28 +0800 Subject: [PATCH] zuoye --- .../0104.md" | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 "48 \351\251\254\345\256\217\350\276\276/0104.md" diff --git "a/48 \351\251\254\345\256\217\350\276\276/0104.md" "b/48 \351\251\254\345\256\217\350\276\276/0104.md" new file mode 100644 index 0000000..d3f5b74 --- /dev/null +++ "b/48 \351\251\254\345\256\217\350\276\276/0104.md" @@ -0,0 +1,147 @@ +## 1. 接收请求中的json数据 + +1.添加json数据转换相关坐标 + +```xml + + com.fasterxml.jackson.core + jackson-databind + 2.11.3 + +``` + +2.开启自动转换json数据的支持 + +```java +@Configuration +@ComponentScan("com.mdd.controller") +// 该注解整合了多个功能,此处仅使用其中一部分功能,即json数据进行自动类型转换 +@EnableWebMvc +public class SpringMvcConfig { +} +``` + +3.设置接收json数据 + +```java +// 接收JSON数据 +@RequestMapping("/listJSON") +@ResponseBody +// @RequestParam 指明为集合,不是对象 +public String getJSON(@RequestBody List list){ + System.out.println("JSON参数传递 ===>"+list); + return "ok"; +} +``` + +4.响应JSON数据 + +```java +// 响应JSON数据 +@RequestMapping("/listResponseJSON") +@ResponseBody +// @RequestParam 指明为集合,不是对象 +public List getResponseJSON(@RequestBody List list){ + System.out.println("JSON参数传递 ===>"+list); + return list; +} +``` + +@RequestParam用于接收url地址传参 + +@RequestBody用于接收json数据 + +5.日期类型参数传递 + +```java +// 时间类型 +@RequestMapping("/data") +@ResponseBody +// @DateTimeFormat 规定日期类型的格式 +public String getData(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date){ + System.out.println(date); + return "ok"; +} +``` + +## 2. REST风格 + +REST(Representational State Transfer),表现形式状态转换 + +- 传统风格资源描述形式 + ​ http://localhost/user/getById?id=1 + ​ http://localhost/user/saveUser + +- REST风格描述形式 + ​ http://localhost/user/1 + ​ http://localhost/user + +优点:隐藏资源的访问行为,无法通过地址得知对资源是何种操作书写简化 + +### 按照REST风格访问资源时使用行为动作区分对资源进行了何种操作 + +http://localhost/users 查询全部用户信息 GET(查询) + +http://localhost/users/1 查询指定用户信息 GET(查询) + +http://localhost/users 添加用户信息 POST(新增/保存 + +http://localhost/users 修改用户信息 PUT(修改/更新) + +http://localhost/users/1 删除用户信息 DELETE(删除) + + + +案例: + +```java +// 使用 REST风格简化代码 +@RestController +//@RequestMapping("users") +public class userController { + + // 查询所有用户 + @RequestMapping(value = "/users",method = RequestMethod.GET) + @ResponseBody + public String selectAll(){ + System.out.println("selectAll is ok..."); + return "selectAll is ok..."; + } + + // 新增用户 + @RequestMapping(value = "/users",method = RequestMethod.POST) + @ResponseBody + public String save(){ + System.out.println("user save is ok..."); + return "user save is ok..."; + } + + // 根据id查询用户 + @RequestMapping(value = "/users/{id}",method = RequestMethod.GET) + @ResponseBody + // @PathVariable 绑定路径参数 + public String selectById(@PathVariable Integer id){ + System.out.println("selectById is ok..."); + return "selectById is ok..."+id; + } + + // 根据id修改用户 + @RequestMapping(value = "/users/{id}",method = RequestMethod.PUT) + @ResponseBody + // @PathVariable 绑定路径参数 + public String updateById(@PathVariable Integer id){ + System.out.println("updateById is ok..."); + return "updateById is ok..."+id; + } + + // 根据id删除用户 + // 根据id修改用户 + @RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE) + @ResponseBody + // @PathVariable 绑定路径参数 + public String deleteById(@PathVariable Integer id){ + System.out.println("deleteById is ok..."); + return "deleteById is ok..."+id; + } +} +``` -- Gitee