SpringBoot:创建三个接口

1.创建项目

 点击next

 

 点击web,选择spring web.

 然后finish就好了。

2.编码部分

 

依次创建bean,controller,manager,service四个包,并且创建Count,ResourceController,ResourceManager,ResourceService类

 如图所示

 

 Count

package com.example.resouce.bean;

public class Count {
    private int count;
      public int getCount() {
                 return count;
           }

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

 ResourceController

package com.example.resouce.controller;
import com.example.resouce.bean.Count;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.resouce.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();
    }
}

ResourceManager

package com.example.resouce.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;
    }
}

ResourceService

package com.example.resouce.service;

import com.example.resouce.bean.Count;
import com.example.resouce.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 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());
        }
    }
}

结果

2.测试

查询接口,服务启动

初始化

  ​​​​​​​

 

 

 修改接口

 

参考文献:https://www.cnblogs.com/wuyizuokan/p/11117294.html

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值