使用Spring Boot创建三个Restful风格的接口

使用Spring Boot创建三个Restful风格的接口

创建项目

这里是项目的目录结构
在这里插入图片描述

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路由请求 可以设置各种操作方法
路由请求@GetMappingGET 方法的路由
路由请求@PostMappingPOST 方法的路由
路由请求@PutMappingPUT 方法的路由
路由请求@DeleteMappingDELETE 方法的路由
请求参数@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 请求方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值