From bfc48cf06e81801b66c723cb591fd6231cf6d198 Mon Sep 17 00:00:00 2001 From: YunaiV <> Date: Sat, 19 Dec 2020 20:03:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20IDEA=20HTTP=20Client=20?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lab-71-idea-http-client/pom.xml | 23 +++++++++ .../iocoder/springboot/lab71/Application.java | 13 +++++ .../lab71/controller/UserController.http | 18 +++++++ .../lab71/controller/UserController.java | 50 +++++++++++++++++++ .../springboot/lab71/vo/UserUpdateVO.java | 43 ++++++++++++++++ lab-71-http-debug/pom.xml | 18 +++++++ pom.xml | 3 +- 7 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 lab-71-http-debug/lab-71-idea-http-client/pom.xml create mode 100644 lab-71-http-debug/lab-71-idea-http-client/src/main/java/cn/iocoder/springboot/lab71/Application.java create mode 100644 lab-71-http-debug/lab-71-idea-http-client/src/main/java/cn/iocoder/springboot/lab71/controller/UserController.http create mode 100644 lab-71-http-debug/lab-71-idea-http-client/src/main/java/cn/iocoder/springboot/lab71/controller/UserController.java create mode 100644 lab-71-http-debug/lab-71-idea-http-client/src/main/java/cn/iocoder/springboot/lab71/vo/UserUpdateVO.java create mode 100644 lab-71-http-debug/pom.xml diff --git a/lab-71-http-debug/lab-71-idea-http-client/pom.xml b/lab-71-http-debug/lab-71-idea-http-client/pom.xml new file mode 100644 index 00000000..04b29abd --- /dev/null +++ b/lab-71-http-debug/lab-71-idea-http-client/pom.xml @@ -0,0 +1,23 @@ + + + + org.springframework.boot + spring-boot-starter-parent + 2.2.1.RELEASE + + + 4.0.0 + + lab-71-idea-http-client + + + + + org.springframework.boot + spring-boot-starter-web + + + + diff --git a/lab-71-http-debug/lab-71-idea-http-client/src/main/java/cn/iocoder/springboot/lab71/Application.java b/lab-71-http-debug/lab-71-idea-http-client/src/main/java/cn/iocoder/springboot/lab71/Application.java new file mode 100644 index 00000000..ba31cf78 --- /dev/null +++ b/lab-71-http-debug/lab-71-idea-http-client/src/main/java/cn/iocoder/springboot/lab71/Application.java @@ -0,0 +1,13 @@ +package cn.iocoder.springboot.lab71; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/lab-71-http-debug/lab-71-idea-http-client/src/main/java/cn/iocoder/springboot/lab71/controller/UserController.http b/lab-71-http-debug/lab-71-idea-http-client/src/main/java/cn/iocoder/springboot/lab71/controller/UserController.http new file mode 100644 index 00000000..0b686258 --- /dev/null +++ b/lab-71-http-debug/lab-71-idea-http-client/src/main/java/cn/iocoder/springboot/lab71/controller/UserController.http @@ -0,0 +1,18 @@ +### 测试 /user/login:登陆成功 +POST http://127.0.0.1:8080/user/login +Content-Type: application/x-www-form-urlencoded + +username=yudaoyuanma&password=123456 + +### 测试 /user/get-current:获取成功 +GET http://127.0.0.1:8080/user/get-current +Authorization: token001 + +### 测试 /user/update:更新成功 +POST http://127.0.0.1:8080/user/update +Content-Type: application/json + +{ + "nickname": "我是昵称", + "gender": 1 +} diff --git a/lab-71-http-debug/lab-71-idea-http-client/src/main/java/cn/iocoder/springboot/lab71/controller/UserController.java b/lab-71-http-debug/lab-71-idea-http-client/src/main/java/cn/iocoder/springboot/lab71/controller/UserController.java new file mode 100644 index 00000000..48c99451 --- /dev/null +++ b/lab-71-http-debug/lab-71-idea-http-client/src/main/java/cn/iocoder/springboot/lab71/controller/UserController.java @@ -0,0 +1,50 @@ +package cn.iocoder.springboot.lab71.controller; + +import cn.iocoder.springboot.lab71.vo.UserUpdateVO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.Map; + +/** + * 用户 Controller + * + * 示例代码,纯粹为了演示。 + */ +@RestController +public class UserController { + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + @PostMapping("/user/login") + public Map login(@RequestParam("username") String username, + @RequestParam("password") String password) { + if ("yudaoyuanma".equals(username) && "123456".equals(password)) { + Map tokenMap = new HashMap<>(); + tokenMap.put("userId", 1); + tokenMap.put("token", "token001"); + return tokenMap; + } + throw new RuntimeException("小朋友,你的账号密码不正确哟!"); + } + + @GetMapping("/user/get-current") + public Map getCurrentUser(@RequestHeader("Authorization") String authorization) { + if ("token001".equals(authorization)) { + Map userInfo = new HashMap<>(); + userInfo.put("id", 1); + userInfo.put("gender", 1); + return userInfo; + } + throw new RuntimeException("小朋友,你没有登录哟!"); + } + + @PostMapping("/user/update") + public Boolean update(@RequestBody UserUpdateVO updateVO) { + logger.info("[update][收到更新请求:{}]", updateVO.toString()); + return true; + } + +} diff --git a/lab-71-http-debug/lab-71-idea-http-client/src/main/java/cn/iocoder/springboot/lab71/vo/UserUpdateVO.java b/lab-71-http-debug/lab-71-idea-http-client/src/main/java/cn/iocoder/springboot/lab71/vo/UserUpdateVO.java new file mode 100644 index 00000000..49eee053 --- /dev/null +++ b/lab-71-http-debug/lab-71-idea-http-client/src/main/java/cn/iocoder/springboot/lab71/vo/UserUpdateVO.java @@ -0,0 +1,43 @@ +package cn.iocoder.springboot.lab71.vo; + +/** + * 用户更新 VO + */ +public class UserUpdateVO { + + /** + * 昵称 + */ + private String nickname; + /** + * 性别 + */ + private Integer gender; + + public String getNickname() { + return nickname; + } + + public UserUpdateVO setNickname(String nickname) { + this.nickname = nickname; + return this; + } + + public Integer getGender() { + return gender; + } + + public UserUpdateVO setGender(Integer gender) { + this.gender = gender; + return this; + } + + @Override + public String toString() { + return "UserUpdateVO{" + + "nickname='" + nickname + '\'' + + ", gender=" + gender + + '}'; + } + +} diff --git a/lab-71-http-debug/pom.xml b/lab-71-http-debug/pom.xml new file mode 100644 index 00000000..74afc145 --- /dev/null +++ b/lab-71-http-debug/pom.xml @@ -0,0 +1,18 @@ + + + + labs-parent + cn.iocoder.springboot.labs + 1.0-SNAPSHOT + + 4.0.0 + + lab-71-http-debug + pom + + lab-71-idea-http-client + + + diff --git a/pom.xml b/pom.xml index 29ec927c..c976242b 100644 --- a/pom.xml +++ b/pom.xml @@ -77,7 +77,7 @@ - lab-70-db-doc + @@ -111,6 +111,7 @@ + lab-71-http-debug