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

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

首先创建一个spring boot项目,详见我的上一篇博客https://blog.csdn.net/weixin_47593895/article/details/120395286?spm=1001.2014.3001.5501

创建项目

创建一个计数器项目,这是项目目录

屏幕截图 2021-09-20 220152.jpg

Count类

package com.example.bean;

public class Count {
      private int count;

              public int getCount() {
                 return count;
             }

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

屏幕截图 2021-09-20 220210.jpg

ResourceController类

@RequestMapping(value = “/example”, method = RequestMethod.PUT)

value: 指定请求的实际地址,指定的地址可以是URI Template 模式(后面将会说明);

method: 指定请求的method类型, GET、POST、PUT、DELETE等;

package com.example.controller;

import com.example.bean.Count;
import com.example.service.ResourceService;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class ResourceController {
    @Resource
    ResourceService resourceService;

    @RequestMapping(value = "/example", method = RequestMethod.PUT)
    @ResponseBody
    public void initCount(@RequestBody Count count) {
        resourceService.initCount(count);
    }

    @RequestMapping(value = "/example", method = RequestMethod.POST)
    @ResponseBody
    public void modifyCount(@RequestBody Count count) {
        resourceService.addCount(count);
    }

    @RequestMapping(value = "/example", method = RequestMethod.GET)
    @ResponseBody
    public Count getCount() {
        return resourceService.getCount();
    }
}

屏幕截图 2021-09-20 220210.jpg

ResourceManger类

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;
    }

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

    public int getCount() {
        return count;
    }

    public void initCount(int i) {
        count = i;
    }
}

屏幕截图 2021-09-20 220520.jpg

ResourceService类

package com.example.service;

import com.example.bean.Count;
import com.example.manager.ResourceManager;

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

    public void minusCount(Count count) {
        if (count != null) {
            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());
        }
    }
}

屏幕截图 2021-09-20 220548.jpg

启动项目

屏幕截图 2021-09-21 175359.jpg

使用postman进行测试

打开postman,file->new->Collection

屏幕截图 2021-09-21 181434.jpg

URL都是:/example,GET用于查询,PUT用于初始化,POST用于修改

启动查询接口,count默认为0

屏幕截图 2021-09-21 181745.jpg

启动初始化接口,初始化count为100

屏幕截图 2021-09-21 181815.jpg

使用GET查询

屏幕截图 2021-09-21 181842.jpg

启动修改接口,count加20

屏幕截图 2021-09-21 181908.jpg

使用GET查询

屏幕截图 2021-09-21 181933.jpg

参考

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值