SpringBoot 入门
1.确定需求
前端访问后端URL,显示后端返回的字符串
输入:
前端访问URL为http://localhost:8080/hello
输出:
前端显示后端返回的字符串Hello SpringBoot
2.新建SpringBoot 项目
3.编写代码
添加Hello.java
编写响应HTTP 请求URI 为/hello 的接口和逻辑
package com.example.test001.controller;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
@RestController
public class HelloWorld {
@GetMapping("/hello")
public String hello(){
return "Hello SpringBoot:"+ nameAges.toString();
}
}
4.编译代码、构建程序
5.执行SpringBoot 程序
查看执行结果
HTTP Client 客户端[浏览器或者Postman]可以通过localhost:8080/hello 来访问对应接口服务
6.测试SpringBoot 后端接口
浏览器测试的开发者工具
Postman 测试
Postman测试
package com.example.test001.controller;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
@RestController
public class HelloWorld {
@GetMapping("/hello")
public String hello(){
return "Hello SpringBoot:"+ nameAges.toString();
}
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 "update name: "+name+", age= "+age;
}
@DeleteMapping("/hello/{name}")
public String helloDel(@PathVariable String name)
{
nameAges.remove(name);
return "delete name: "+name;
}
}
HTTP GET
HTTP POST
HTTP PUT
HTTP DELETE
刷新查询数据
进行删除
刷新看是否删除成功