mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 IDEA HTTP Client 示例代码
This commit is contained in:
23
lab-71-http-debug/lab-71-idea-http-client/pom.xml
Normal file
23
lab-71-http-debug/lab-71-idea-http-client/pom.xml
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.1.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-71-idea-http-client</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 实现对 Spring MVC 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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<String, Object> login(@RequestParam("username") String username,
|
||||
@RequestParam("password") String password) {
|
||||
if ("yudaoyuanma".equals(username) && "123456".equals(password)) {
|
||||
Map<String, Object> tokenMap = new HashMap<>();
|
||||
tokenMap.put("userId", 1);
|
||||
tokenMap.put("token", "token001");
|
||||
return tokenMap;
|
||||
}
|
||||
throw new RuntimeException("小朋友,你的账号密码不正确哟!");
|
||||
}
|
||||
|
||||
@GetMapping("/user/get-current")
|
||||
public Map<String, Object> getCurrentUser(@RequestHeader("Authorization") String authorization) {
|
||||
if ("token001".equals(authorization)) {
|
||||
Map<String, Object> 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
18
lab-71-http-debug/pom.xml
Normal file
18
lab-71-http-debug/pom.xml
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>labs-parent</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-71-http-debug</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>lab-71-idea-http-client</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
3
pom.xml
3
pom.xml
@@ -77,7 +77,7 @@
|
||||
<!-- <module>lab-67</module>-->
|
||||
<!-- <module>lab-68-spring-security-oauth</module>-->
|
||||
<!-- <module>lab-69-proxy</module>-->
|
||||
<module>lab-70-db-doc</module>
|
||||
<!-- <module>lab-70-db-doc</module>-->
|
||||
|
||||
<!-- Spring Cloud 示例 -->
|
||||
<!-- <module>labx-01-spring-cloud-alibaba-nacos-discovery</module>-->
|
||||
@@ -111,6 +111,7 @@
|
||||
<!-- <module>labx-28</module>-->
|
||||
<!-- <module>labx-29-spring-cloud-consul-bus</module>-->
|
||||
<!-- <module>labx-30-spring-cloud-grpc</module>-->
|
||||
<module>lab-71-http-debug</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
||||
Reference in New Issue
Block a user