首页 > 编程语言 >《java接力》springboot篇——新建项目

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

时间:2023-05-08 10:59:10浏览次数:37  
标签:count me java springboot ResourceManager Count 接力 com public

新建项目

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

新建项目

image

image

image

无法链接spring官网

法一:可以使用 https://start.springboot.io/
法二:使用自定义网址,把那个网址的https后面的s去掉,使用http开头就可以访问和使用了。

代码

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

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

标签:count,me,java,springboot,ResourceManager,Count,接力,com,public
From: https://www.cnblogs.com/fusio/p/17381055.html

相关文章

  • SpringBoot自动配置原理
    SpringBoot自动配置原理一、什么是SpringBoot的自动配置?SpringBoot的最大的特点就是简化了各种xml配置内容,还记得曾经使用SSM框架时我们在spring-mybatis.xml配置了多少内容吗?数据源、连接池、会话工厂、事务管理···,而现在SpringBoot告诉你这些都不需要了,一切交给它的自动......
  • SpringBoot运行流程
    SpringBoot运行流程一、准备阶段我们先看一下这个SpringApplication的构造方法中做了什么事情,为run方法准备了那些事情通常在一个springboot的应用中,会看到下面一段代码作为应用的入口@SpringBootApplicationpublicclassApplication{publicstaticvoidmain(String......
  • Java入门7(异常处理,list集合)
    异常处理(try-catch)错误的种类一般来讲,程序出现错误的时候,大致情况有三种:语法错误运行时错误,指的是各程序运行的时候,出现的一些没有想到的问题,比如除数为0,比如数组下标越界等等逻辑错误,运行结果和与其结果不一致,俗称bug⭐Java中的异常处理机制主要用于处理运行时错误异常......
  • Java面试-框架篇九
    1、spring的理解Spring是实现了工厂模式的工程类,这个类名为BeanFactory(实际是一个接口),在程序中通常BeanFactory的子类ApplicatioContext。Spring相当于一个大的工厂类,在其配置文件中通过元素配置用于创建实例对象的类名和实例对象的属性。Spring提供了对IOC良好支持,IOC是一种编程思......
  • JavaScript fromCharCode() 方法
    fromCharCode()方法返回指定的Unicode编码对应的字符。语法格式:String.fromCharCode(n1,n2,...)参数:n1,n1,..表示指定的Unicode编码。示例:(1)返回指定Unicode编码的字符:<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8">&......
  • Java 三方接口PHP写法;doHmacSHA2; 将字节数组转换成16进制字符串;Mac.getInstance;Hma
    先看一段Java代码,一个签名过程1packagecom.sixents.bss.filter;234importorg.apache.http.HttpEntity;5importorg.apache.http.NameValuePair;6importorg.apache.http.client.entity.UrlEncodedFormEntity;7importorg.apache.http.client.met......
  • SpringBoot版本接口
    SpringBoot版本接口前言为什么接口会出现多个版本一般来说,RestfulAPI接口是提供给其它模块,系统或是其他公司使用,不能随意频繁的变更。然而,需求和业务不断变化,接口和参数也会发生相应的变化。如果直接对原来的接口进行修改,势必会影响线其他系统的正常运行。这就必须对api接口......
  • SpringBoot参数校验
    SpringBoot参数校验为什么需要参数校验在日常的接口开发中,为了防止非法参数对业务造成影响,经常需要对接口的参数进行校验,例如登录的时候需要校验用户名和密码是否为空,添加用户的时候校验用户邮箱地址、手机号码格式是否正确。靠代码对接口参数一个个校验的话就太繁琐了,代码可读......
  • 29基于java的在线考试系统设计与实现
    本章节来介绍一个基于java的在线考试系统的实现系统概要近年来,随着世界各国需要参加考核的人员与日俱增,单纯依靠传统的人工安排考场和监考人员的纸质化考试逐渐显示出了效率低,易发生冲突的缺陷,这时,在线考试系统便应运而生,此种考试方式以方便快捷高效等优点将越来越适用于如今......
  • SpringBoot统一异常处理
    SpringBoot统一异常处理概述Spring在3.2版本增加了一个注解@ControllerAdvice,可以与@ExceptionHandler、@InitBinder、@ModelAttribute等注解注解配套使用。简单的说,该注解可以把异常处理器应用到所有控制器,而不是单个控制器。借助该注解,我们可以实现:在独立的某个地方,比如单独......