一文带你搞懂Java中Get和Post的使用
作者:代码的路 时间:2023-04-16 23:57:24
1 Get请求数据
项目地址:https://github.com/Snowstorm0/learn-get-post
1.1 Controller
文件名MyController,内容为:
@RestController
@RequestMapping("/homepage")
publicclass MyController {
@Autowired
MyService myService;
@GetMapping("/learnGet")
public String learnGet(){
return myService.learnGet();
}
}
1.2 Service
文件名MyService,内容为:
@Service
@EnableScheduling
publicclass MyService {
public String learnGet(){
Long timeLong = System.currentTimeMillis();
SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置格式
String timeString = timeFormat.format(timeLong);
return timeString;
}
}
1.3 Application
在application.properties配置:
# 设置端口号
server.port=8888
1.4 Postman
配置Get,地址为:http://localhost:8888/homepage/returnTime 。
即可获得当前时间戳。
2 Post接收数据
项目地址:https://github.com/Snowstorm0/learn-get-post
2.1 Controller
文件名MyController,内容为:
@RestController
@RequestMapping("/homepage")
publicclass MyController {
@Autowired
MyService myService;
@PostMapping("/postReceive")
public Map<String, Object> postReceive(@RequestParam("number") int number, @RequestParam("name") String name) {
return myService.postReceive(number, name);
}
@PostMapping("/postReceiveByMap")
public Map<String, Object> postReceiveByMap(@RequestParam Map<String, Object> map) {
System.out.println("map:" + map + "\n");
return myService.postReceiveByMap(map);
}
}
2.2 Service
文件名MyService,内容为:
@Service
@EnableScheduling
publicclass MyService {
public Map<String, Object> postReceive(int number, String name){
Map<String, Object> res = new HashMap<>();
res.put("number", number);
res.put("name", name);
return res;
}
public Map<String, Object> postReceiveByMap(Map<String, Object> map){
int number = map.get("number") == null ? 0 : Integer.parseInt((String) map.get("number"));
String name = map.get("name") == null ? "" : (String)map.get("name");
Map<String, Object> res = new HashMap<>();
res.put("number", number);
res.put("name", name);
System.out.println("map:" + map + "\n");
System.out.println("res:" + res + "\n");
return res;
}
2.3 Application
在application.properties配置:
# 设置端口号
server.port=8888
2.4 Postman
配置Get,地址为:http://localhost:8888/homepage/returnTime 。
即可获得输出。
3 Post发送数据
项目地址:https://github.com/Snowstorm0/learn-post-send
需要注意,RestTemplate在postForObject时,用MultiValueMap,不可使用HashMap。
3.1 Controller
文件名MyController,内容为:
@RestController
@RequestMapping("/homepage")
publicclass MyController {
@Autowired
MyService myService;
@PostMapping("/postSend")
public Map<String, Object> postSend() {
return myService.postSend();
}
}
3.2 Service
文件名MyService,内容为:
@Service
@EnableScheduling
publicclass MyService {
@Resource
private RestTemplate restTemplate;
String URL = "http://localhost:8888/homepage/postReceiveByMap";
public Map<String, Object> postSend(){
Map<String, Object> sendData = new HashMap<>();
sendData.put("number", 3);
sendData.put("name", "张三");
ResponseEntity<ResponseResult> responseData = restTemplate.postForEntity(URL, sendData, ResponseResult.class);
Map<String, Object> returnData = new HashMap<>();
returnData.put("StatusCode:", responseData.getStatusCode());
returnData.put("Body:", responseData.getBody());
return returnData;
}
}
3.3 ResponseResult
publicclass ResponseResult {
privateint number;
private String name;
public ResponseResult(){
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return"ResponseResult [number=" + number + ",name=" + name + "]";
}
}
3.4 Config
@Configuration
publicclass Config {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder){
return builder.build();
}
}
3.5 Application
在application.properties配置:
# 设置端口号
server.port=8889
3.6 Postman
配置Post,地址为: http://localhost:8889/homepage/postSend
即可获得输出。
来源:https://mp.weixin.qq.com/s/mC0D1nuCqIori5bWtLorWQ
标签:Java,Get,Post
0
投稿
猜你喜欢
Java基础教程之组合(composition)
2022-08-02 19:12:32
在Spring Boot中加载初始化数据的实现
2023-08-18 10:01:44
Android中Blade的使用方法
2023-04-25 11:29:54
C++数组的定义详情
2023-07-21 08:00:49
Flutter混合开发详解
2023-05-15 11:10:54
Spring Security使用Lambda DSL配置流程详解
2021-12-23 19:20:32
Java Apollo是如何实现配置更新的
2023-09-26 13:55:18
Android自定义评分控件的完整实例
2021-10-26 19:25:58
Android EventBus 3.0.0 使用总结(必看篇)
2023-09-06 06:32:41
C#操作串口通信协议Modbus的常用方法介绍
2023-01-20 04:06:58
java新手入门——String类详解
2022-02-23 08:17:26
Android通过aapt命令获取apk详细信息(包括:文件包名,版本号,SDK等信息)
2023-11-01 11:29:19
Java 添加超链接到 Word 文档方法详解
2023-01-29 08:49:30
SpringMVC 参数绑定相关知识总结
2022-06-05 12:50:54
使用chatgpt实现微信聊天小程序的代码示例
2022-04-26 17:18:24
Android自定义dialog简单实现方法
2021-07-29 17:10:00
C#调用WebService的方法介绍
2022-06-05 01:10:30
Android 添加系统设置属性的实现及步骤
2021-12-31 01:11:02
Unity 制作一个分数统计系统
2021-11-30 03:01:52
Android软键盘挡住输入框的终极解决方案
2022-04-30 01:16:47