From 9b252b406884c67af9c4cff40761ddc4d2bedf22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=98=8E=E5=81=A5?= <2161737470@qq.com> Date: Thu, 4 Jan 2024 10:58:14 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=8D=81=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...44\270\216REST\351\243\216\346\240\274.md" | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 "04 \346\235\216\346\230\216\345\201\245/20240103 \350\257\267\346\261\202\345\223\215\345\272\224JSON\346\225\260\346\215\256\344\270\216REST\351\243\216\346\240\274.md" diff --git "a/04 \346\235\216\346\230\216\345\201\245/20240103 \350\257\267\346\261\202\345\223\215\345\272\224JSON\346\225\260\346\215\256\344\270\216REST\351\243\216\346\240\274.md" "b/04 \346\235\216\346\230\216\345\201\245/20240103 \350\257\267\346\261\202\345\223\215\345\272\224JSON\346\225\260\346\215\256\344\270\216REST\351\243\216\346\240\274.md" new file mode 100644 index 0000000..6b37b18 --- /dev/null +++ "b/04 \346\235\216\346\230\216\345\201\245/20240103 \350\257\267\346\261\202\345\223\215\345\272\224JSON\346\225\260\346\215\256\344\270\216REST\351\243\216\346\240\274.md" @@ -0,0 +1,152 @@ +## 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