基于Spring Boot实现RESTful接口

一、HTTP协议相关介绍

1、HTTP工作原理

  HTTP协议定义Web客户端如何从Web服务器请求Web页面,以及服务器如何把Web页面传送给客户端。HTTP协议采用了请求/响应模型。客户端向服务器发送一个请求报文,请求报文包含请求的方法、URL、协议版本、请求头部和请求数据。服务器以一个状态行作为响应,响应的内容包括协议的版本、成功或者错误代码、服务器信息、响应头部和响应数据。
  HTTP 协议采用请求/响应模型。客户端向服务器发送一个请求报文,服务器以一个状态作为响应。

2、HTTP请求方法

  • HTTP/1.1协议中共定义了八种方法(有时也叫“动作”),来表明Request-URL指定的资源不同的操作方式
  • HTTP1.0定义了三种请求方法:GET, POSTHEAD方法。
  • HTTP1.1新增了五种请求方法:OPTIONS, PUT, DELETE, TRACECONNECT 方法
请求方法详情
GET向指定的资源发出“显示”请求。使用GET方法应该只用在读取数据,而不应当被用于产生“副作用”的操作中,例如在Web Application中。其中一个原因是GET可能会被网络蜘蛛等随意访问。
HEAD与GET方法一样,都是向服务器发出指定资源的请求。只不过服务器将不传回资源的本文部分。它的好处在于,使用这个方法可以在不必传输全部内容的情况下,就可以获取其中“关于该资源的信息”(元信息或称元数据)。
POST向指定资源提交数据,请求服务器进行处理(例如提交表单或者上传文件)。数据被包含在请求本文中。这个请求可能会创建新的资源或修改现有资源,或二者皆有。
PUT向指定资源位置上传其最新内容。
DELETE请求服务器删除Request-URI所标识的资源。
TRACE回显服务器收到的请求,主要用于测试或诊断。
OPTIONS这个方法可使服务器传回该资源所支持的所有HTTP请求方法。用’*'来代替资源名称,向Web服务器发送OPTIONS请求,可以测试服务器功能是否正常运作。
CONNECTHTTP/1.1协议中预留给能够将连接改为管道方式的代理服务器。通常用于SSL加密服务器的链接(经由非加密的HTTP代理服务器)。

二、RESTful接口的使用

1、RESTful简介

  REST(Representational State Transfer)表述性状态传递,决定了接口的形式与规则。RESTful是基于http方法的API设计风格。

2、编写Spring Boot项目

1、创建Spring Boot项目
在这里插入图片描述
2、创建bean,controller,manager,service四个包
在这里插入图片描述
3、在四个包下面分别创建对应的类
在这里插入图片描述

package com.example.demo.bean;
public class Count {
    private int count;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
package com.example.demo.controller;

import com.example.demo.bean.Count;
import com.example.demo.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@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();
    }

}

package com.example.demo.manager;
public class ResourceManager {

    private int count = 0;

    private static ResourceManager instance = new ResourceManager();

    private ResourceManager() {

    }

    public static ResourceManager getInstance(){
        return instance;
    }

    public void initCount(int count){
        this.count = count;
    }

    public int getCount(){
        return count;
    }

    public synchronized void addCount(int count){
        this.count += count;
    }

    public synchronized void minusCount(int count){
        this.count -= count;
    }

}

package com.example.demo.service;

import com.example.demo.bean.Count;
import com.example.demo.manager.ResourceManager;
import org.springframework.stereotype.Service;

@Service
public class ResourceService {
    public void addCount(Count count){
        ResourceManager.getInstance().addCount(count.getCount());
    }

    public void minusCount(Count count){
        ResourceManager.getInstance().minusCount(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());
        }
    }
}

4、运行项目
在这里插入图片描述

三、使用Postman进行测试

新建Wordspaces
在这里插入图片描述
点击+
在这里插入图片描述

选择Get,输入url为http://localhost:8080/me/count,点击Send
在这里插入图片描述

结果如下:
在这里插入图片描述

Post
在这里插入图片描述

post后并用get查询:
在这里插入图片描述
PUT:
在这里插入图片描述

最后用GET查询:
在这里插入图片描述

四、相关参考

https://www.jianshu.com/p/9b8b9672b8e6
https://www.cnblogs.com/an-wen/p/11180076.html
https://www.kancloud.cn/hanxt/springboot2/1177585
https://www.cnblogs.com/wuyizuokan/p/11117294.html

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值