Springboot 初步入门,并建立restful接口

目录

一,Springboot简介

二,项目建立


一,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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值