IDEA创建一个SpringBoot并创建RESTful接口的Web服务

一、使用IntellijIDEA建立第一个spring boot 项目

1、创建项目

打开IDEA,点击file–>new–>project:

在这里插入图片描述
左边选择spring initializr,再next:
在这里插入图片描述
group和artifact可以自己取,然后把java version改成8,点击next:
在这里插入图片描述
选择web,再选web spring,点击next,这样就创建完成了:
在这里插入图片描述
创建完成了后,再创建4个包,每个包 里建一个java文件(如下图):
在这里插入图片描述

2、代码和程序调试

Count的代码:

package com.helloworld.boothello.bean;
 public class Count {
     private int count;

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

Manage的代码:

package com.helloworld.boothello.manager;

public class Manager {
    private int count = 0;

    private static Manager instance = new Manager();

    private Manager(){}

    public static Manager 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;
    }
}

Control的代码:

package com.helloworld.boothello.Controller;

import com.helloworld.boothello.bean.Count;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.helloworld.boothello.Service.service;
@RestController
public class Control {

    @Autowired
    service Services;

    @RequestMapping(value = "/me/count", method = RequestMethod.PUT)
    @ResponseBody
    public void initCount(@RequestBody Count count){
        Services.initCount(count);
    }

    @RequestMapping(value = "/me/count", method = RequestMethod.POST)
    @ResponseBody
    public void modifyCount(@RequestBody Count count){
        Services.addCount(count);
    }

    @RequestMapping(value = "/me/count", method = RequestMethod.GET)
    @ResponseBody
    public  Count getCount()
    {
        return Services.getCount();
    }
}

service的代码:

package com.helloworld.boothello.Service;


import com.helloworld.boothello.bean.Count;
import com.helloworld.boothello.manager.Manager;
import org.springframework.stereotype.Service;

@Service
public class service {
    public void addCount(Count count){
        if (count != null){
            Manager.getInstance().addCount(count.getCount());
        }
    }

    public void minusCount(Count count){
        if (count != null) {
            Manager.getInstance().minusCount(count.getCount());
        }
    }

    public Count getCount()
    {
        Count count = new Count();
        count.setCount(Manager.getInstance().getCount());
        return count;
    }

    public void initCount(Count count){
        if (count != null) {
            Manager.getInstance().initCount(count.getCount());
        }
    }
}

BootHelloApplication的代码是创建后就有的,不用改变,右键调试代码,输出可以看到端口是8888:
在这里插入图片描述

二、用postman来测试

web网页的测试也可以用postman软件来测试,在客户端可以分别用get、put、post、delete等测试该web服务资源时,服务器能做出正确的结果,推荐在postman官网下载。

get的应用

get–>send,得到count初值为0:
在这里插入图片描述

post的应用

可以改变初值,按下图步骤可以给count改变初值:
在这里插入图片描述
然后用get查看count的值,发现变成了我们赋值的值:
在这里插入图片描述

put的应用

put是改变值,将值改变再send:
在这里插入图片描述
用get查一下:
在这里插入图片描述
也可以加一个负数:
在这里插入图片描述
再用get查一下:
在这里插入图片描述

delete的应用

删除肯定有相应的参数传给后端,比如id,根据记录的id进行删除,在这里想要提一下物理删除和逻辑删除:
物理删除:那就是,把数据彻底的删除掉,数据库就不会存在那条数据啦;
逻辑删除:数据表会有个is delete字段,当想要删除的时候,将它设为Y,当需要提供数据给前端的时候遍历这个字段,若是已经是Y的那就不能返回给前端。
逻辑删除是一个保留数据的很好方式,邮件也会有那种,删除,还可以回复的那种,但是彻底删除那就不可以了,对一个公司来说,删除掉的数据也是有用的,还是可以进行数据分析的。

三、参考链接

1、字母哥博客:手摸手教你学spring boot 2.x
2、Spring Boot 使用IDEA创建一个 Spring Boot 服务,并创建三个restful风格的接口
3、postman的四种请求:post,get,put,delete

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值