目录
新建SpingBoot项目
项目结构介绍
主程序介绍
编写实例代码
该代码用于演示HTTP GET、POST、PUT、DELETE 请求
package com.example.spingboottask.controller;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
@RestController
public class HelloWorld {
@GetMapping("/hello")
public String helloGet(){
return "Hello SpingBoot"+nameAges.toString();
}
//一个哈希映射(HashMap)的实例
//HashMap 是 Java 中的一种数据结构,用于存储键-值对,它允许你通过键来查找对应的值
//nameAges 是一个 HashMap,键是字符串(String),值是整数(Integer)。
private static HashMap<String,Integer> nameAges = new HashMap<>();
@PostMapping("/hello")
public String helloPost(String name,int age){
nameAges.put(name,age);
return "add name:" + name +", age:"+age;
}
@PutMapping("/hello")
public String helloPut(String name,int age){
nameAges.replace(name,age);
return "updata name:" + name+".age:"+age;
}
@DeleteMapping("/hello/{name}")
public String helloDel(@PathVariable String name){
nameAges.remove(name);
return "delete name:" + name;
}
}
Postman 测试
因为浏览器只能发起http get请求,测试方式有限,所以用postman测试post、push、del
GET请求
POST请求
PUT请求
用get请求查看数据是否正常
DELETE请求
用get请求查看数据是否正常