创建项目
这里是项目的目录结构
bean
在此包下面创建一个Count类
package com.example.bean;
public class Count {
private int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
controller
在此包下面创建一个ResourceController类
package com.example.controller;
import com.example.bean.Count;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.service.ResourceService;
@RestController
public class ResourceController {
@Autowired
ResourceService resourceService;
@RequestMapping(value = "/me/count", method = RequestMethod.PUT)
@ResponseBody
public void initCount(@RequestBody Count count){
resourceService.initCount(count);
}
@RequestMapping(value = "/me/count", method = RequestMethod.POST)
@ResponseBody
public void modifyCount(@RequestBody Count count){
resourceService.addCount(count);
}
@RequestMapping(value = "/me/count", method = RequestMethod.GET)
@ResponseBody
public Count getCount()
{
return resourceService.getCount();
}
}
这里要注意在 Spring Boot 中使用到 @Controller 及相关的注解如下,主要分为三个层面进行,请求前,处理中,返回。
应用场景 | 注解 | 注解说明 |
---|---|---|
处理请求 | @Controller | 处理 Http 请求 |
处理请求 | @RestController | @Controller 的衍生注解 |
路由请求 | @RequestMapping | 路由请求 可以设置各种操作方法 |
路由请求 | @GetMapping | GET 方法的路由 |
路由请求 | @PostMapping | POST 方法的路由 |
路由请求 | @PutMapping | PUT 方法的路由 |
路由请求 | @DeleteMapping | DELETE 方法的路由 |
请求参数 | @PathVariable | 处理请求 url 路径中的参数 /user/{id} |
请求参数 | @RequestParam | 处理问号后面的参数 |
请求参数 | @RequestBody | 请求参数以json格式提交 |
返回参数 | @ResponseBody | 返回 json 格式 |
manager
在此包下面创建一个ResourceManager类
package com.example.manager;
public class ResourceManager {
private int count = 0;
private static ResourceManager instance = new ResourceManager();
private ResourceManager(){}
public static ResourceManager getInstance(){
return instance;
}
public synchronized void addCount(int i){
count = count + i;
System.out.println("addCount success");
}
public int getCount(){
return count;
}
public void initCount(int i){
count = i;
System.out.println("inti success");
}
}
service
在此包下面创建一个 ResourceService类
package com.example.service;
import com.example.bean.Count;
import com.example.manager.ResourceManager;
import org.springframework.stereotype.Service;
@Service
public class ResourceService {
public void addCount(Count count){
if (count != null){
ResourceManager.getInstance().addCount(count.getCount());
}
}
public Count getCount()
{
Count count = new Count();
count.setCount(ResourceManager.getInstance().getCount());
return count;
}
public void initCount(Count count){
if (count != null) {
ResourceManager.getInstance().initCount(count.getCount());
}
}
}
接口测试
下面打开Postman,new一个API接口测试
GET:一般用于检索查询数据,请求指定的信息,服务器根据参数返回指定的实体,参数包含在url里面,比如:http://localhost:8080/me/count/?xxx=xxx&xxx=xxx
POST: 一般是用于向服务器提交数据进行处理,该方式常用来数据的提交等新增操作的,数据被包含在请求的body中
PUT:与post类似,该请求用于对服务器资源进行修改,从客户端向服务器传送数据取代指定的文档的内容,该请求侧重于内容的更新
DELETE:请求删除服务器的某些资源
之后输入url:http://localhost:8080/me/count 发送GET请求,可以看到返回的body消息体中“count”的值为初始化的0(count的值是在ResourceManage类中初始化的)
发送PUT请求
再次使用查询接口count变为了100
发送POST请求
再次使用查询接口count变为了200
成功使用Postman测试GET,PUT,POST三种请求
参考文章:
1.【SpringBoot】使用IDEA创建一个SpringBoot服务,并创建三个restful风格的接口
2.Spring Boot Web 开发@Controller @RestController 使用教程
3.HTTP 请求方法