新建项目
参考链接:https://www.cnblogs.com/wuyizuokan/p/11117294.html
参考链接:https://segmentfault.com/q/1010000007938655
新建项目
无法链接spring官网
法一:可以使用 https://start.springboot.io/
法二:使用自定义网址,把那个网址的https后面的s去掉,使用http开头就可以访问和使用了。
代码
演示的功能就是提供一个计数器功能,可以初始化计数器,修改计数器,查询计数器当前值。没有使用数据库,直接用一个单例类来模拟了,项目结构如下:
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;
}
}
点击查看代码
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类上右键启动:
服务启动正常:
测试
服务提供了三个接口:
URL都是:/me/count 只是分PUT、POST和GET,其中PUT用于初始化,POST用于修改(这里修改是累加),GET用于查询。
下面使用POSTMan进行测试:
查询接口,服务启动,count默认就是0:
初始化:
再次使用查询接口:
修改接口:
修改后查询:
标签:count,me,java,springboot,ResourceManager,Count,接力,com,public From: https://www.cnblogs.com/fusio/p/17381055.html