Spring Boot开发入门——RESTful 接口的 Web服务

1. 创建SpringBoot项目

参考SpringBoot简单开发web

2. 代码开发

2.1目的

提供一个计数器的功能,初始化计数器,修改计数器,查询计数器当前值。

2.2 创建

在com.example.demo下创建三个包,在包下创建三个java类

  • 右击com.example.demo,选择new->package(注意要想在com.example.demo下创建取包名时前面包名的不能删除)

在这里插入图片描述

  • 在前面创建的包下面创建一个类

  • 依次进行,最后项目如下
    在这里插入图片描述

2.3 代码如下

  • Count类
    在这里插入图片描述
package com.example.demo.bean;

public class Count {
    private int count;

    public int getCount() {
        return count;
    }

    public void setCount(int count){
        this.count=count;
    }
}
  • controller类
    在这里插入图片描述
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 controller {
    @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();
    }

}
  • ResourceManager类
    在这里插入图片描述
package com.example.demo.manager;

public class ResourceManager {


    public static ResourceManager getInstance;
    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;
    }
    public synchronized void minusCount(int i){
        count= count-i;
    }
    public int getCount(){
        return count;
    }
}
  • ResourceService类
    在这里插入图片描述
package com.example.demo.service;

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

import java.awt.*;

@Service
public class ResourceService {


    public void initCount(Count count) {
        if (count != null){
            ResourceManager.getInstance().addCount(count.getCount());
        }
    }

    public void addCount(Count count) {
        if (count!=null){
            ResourceManager.getInstance.minusCount(count.getCount());
        }
    }

    public Count getCount() {
        Count count = new Count();
        count.setCount(ResourceManager.getInstance().getCount());
        return count;
    }
}

3. 代码运行

运行正常
在这里插入图片描述

4. 接口测试

4.1 测试工具

工具:Postman
官网下载
点击在这里插入图片描述
在这里插入图片描述

4.2 方法

URL: localhost:8080/me/count,其中PUT用于初始化,POST用于修改(这里修改是累加),GET用于查询(PUT,POST要以Json格式发送参数给web)
查询接口,服务启动,count默认就是0:
在这里插入图片描述
初始化(PUT):
在这里插入图片描述
点击send

再次查询(GET),结果如下:
在这里插入图片描述
修改(加)接口(POST):
在这里插入图片描述
再次查询(GET),结果如下:
在这里插入图片描述
加一个负数(-100)看看结果:
在这里插入图片描述

查询:

在这里插入图片描述
测试完成!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值