《java接力》springboot篇——新建项目

发布时间 2023-05-08 10:53:37作者: Fusio

新建项目

参考链接:https://www.cnblogs.com/wuyizuokan/p/11117294.html

新建项目

image

image

image

代码

演示的功能就是提供一个计数器功能,可以初始化计数器,修改计数器,查询计数器当前值。没有使用数据库,直接用一个单例类来模拟了,项目结构如下:

image

Count:

点击查看代码
package com.me.redis.resouce.bean;

public class Count {
    private int count;

    public int getCount() {
        return count;
    }

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

ResourceController:

点击查看代码
package com.me.redis.resouce.controller;

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

ResourceService:

点击查看代码
package com.me.redis.resouce.service;

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

ResourceManager:

点击查看代码
package com.me.redis.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;
    }
}
ResouceApplication是idea自动生成的:
点击查看代码
package com.me.redis.resouce;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ResouceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ResouceApplication.class, args);
    }

}

启动服务

在ResourceApplication类上右键启动:
image

服务启动正常:

image

测试

服务提供了三个接口:

URL都是:/me/count 只是分PUT、POST和GET,其中PUT用于初始化,POST用于修改(这里修改是累加),GET用于查询。

下面使用POSTMan进行测试:

查询接口,服务启动,count默认就是0:
image

初始化:
image

再次使用查询接口:

image

修改接口:

image

修改后查询:

image