Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bfc48cf

Browse files
author
YunaiV
committed
增加 IDEA HTTP Client 示例代码
1 parent 8652a4c commit bfc48cf

File tree

7 files changed

+167
-1
lines changed

7 files changed

+167
-1
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.2.1.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>lab-71-idea-http-client</artifactId>
14+
15+
<dependencies>
16+
<!-- 实现对 Spring MVC 的自动化配置 -->
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-web</artifactId>
20+
</dependency>
21+
</dependencies>
22+
23+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springboot.lab71;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
### 测试 /user/login:登陆成功
2+
POST http://127.0.0.1:8080/user/login
3+
Content-Type: application/x-www-form-urlencoded
4+
5+
username=yudaoyuanma&password=123456
6+
7+
### 测试 /user/get-current:获取成功
8+
GET http://127.0.0.1:8080/user/get-current
9+
Authorization: token001
10+
11+
### 测试 /user/update:更新成功
12+
POST http://127.0.0.1:8080/user/update
13+
Content-Type: application/json
14+
15+
{
16+
"nickname": "我是昵称",
17+
"gender": 1
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cn.iocoder.springboot.lab71.controller;
2+
3+
import cn.iocoder.springboot.lab71.vo.UserUpdateVO;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
/**
12+
* 用户 Controller
13+
*
14+
* 示例代码,纯粹为了演示。
15+
*/
16+
@RestController
17+
public class UserController {
18+
19+
private final Logger logger = LoggerFactory.getLogger(getClass());
20+
21+
@PostMapping("/user/login")
22+
public Map<String, Object> login(@RequestParam("username") String username,
23+
@RequestParam("password") String password) {
24+
if ("yudaoyuanma".equals(username) && "123456".equals(password)) {
25+
Map<String, Object> tokenMap = new HashMap<>();
26+
tokenMap.put("userId", 1);
27+
tokenMap.put("token", "token001");
28+
return tokenMap;
29+
}
30+
throw new RuntimeException("小朋友,你的账号密码不正确哟!");
31+
}
32+
33+
@GetMapping("/user/get-current")
34+
public Map<String, Object> getCurrentUser(@RequestHeader("Authorization") String authorization) {
35+
if ("token001".equals(authorization)) {
36+
Map<String, Object> userInfo = new HashMap<>();
37+
userInfo.put("id", 1);
38+
userInfo.put("gender", 1);
39+
return userInfo;
40+
}
41+
throw new RuntimeException("小朋友,你没有登录哟!");
42+
}
43+
44+
@PostMapping("/user/update")
45+
public Boolean update(@RequestBody UserUpdateVO updateVO) {
46+
logger.info("[update][收到更新请求:{}]", updateVO.toString());
47+
return true;
48+
}
49+
50+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cn.iocoder.springboot.lab71.vo;
2+
3+
/**
4+
* 用户更新 VO
5+
*/
6+
public class UserUpdateVO {
7+
8+
/**
9+
* 昵称
10+
*/
11+
private String nickname;
12+
/**
13+
* 性别
14+
*/
15+
private Integer gender;
16+
17+
public String getNickname() {
18+
return nickname;
19+
}
20+
21+
public UserUpdateVO setNickname(String nickname) {
22+
this.nickname = nickname;
23+
return this;
24+
}
25+
26+
public Integer getGender() {
27+
return gender;
28+
}
29+
30+
public UserUpdateVO setGender(Integer gender) {
31+
this.gender = gender;
32+
return this;
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return "UserUpdateVO{" +
38+
"nickname='" + nickname + '\'' +
39+
", gender=" + gender +
40+
'}';
41+
}
42+
43+
}

‎lab-71-http-debug/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>labs-parent</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-71-http-debug</artifactId>
13+
<packaging>pom</packaging>
14+
<modules>
15+
<module>lab-71-idea-http-client</module>
16+
</modules>
17+
18+
</project>

‎pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
<!-- <module>lab-67</module>-->
7878
<!-- <module>lab-68-spring-security-oauth</module>-->
7979
<!-- <module>lab-69-proxy</module>-->
80-
<module>lab-70-db-doc</module>
80+
<!-- <module>lab-70-db-doc</module>-->
8181

8282
<!-- Spring Cloud 示例 -->
8383
<!-- <module>labx-01-spring-cloud-alibaba-nacos-discovery</module>-->
@@ -111,6 +111,7 @@
111111
<!-- <module>labx-28</module>-->
112112
<!-- <module>labx-29-spring-cloud-consul-bus</module>-->
113113
<!-- <module>labx-30-spring-cloud-grpc</module>-->
114+
<module>lab-71-http-debug</module>
114115
</modules>
115116

116117
</project>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /