目录
一,Springboot简介
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
二,项目建立
我使用的是IDEA
点击file-new-project,在这里选择Spring initializr,然后选择Default,点击next
然后修改group名字,group名字修改是name也会跟着改变,然后选择java版本,我选的是8
创建完成后需要自己再去建立几个包,和建立java包一样,右键new一个package,然后再在该包下新建一个java文件,总共要建四个包,四个java文件
建好之后该如图所示
然后是以下几个java文件中的代码
Count.java
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.Java
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;
import org.springframework.web.bind.annotation.RestController;
@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.java
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.java
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());
}
}
}
ResouceApplication.java是自动生成的,不需要修改
然后代码成功运行,运行结果为:
让他一直保持运行状态,这时我们再去postman当中进行接口调试。
postman调试链接https://blog.csdn.net/qq_53329531/article/details/120357039