SpringBoot简单入门之GET、PUT和POST请求处理

本文介绍了如何使用SpringBoot构建一个简单的Web应用,处理HTTP的GET、POST和PUT请求。当GET请求时,初始化数字为0;POST请求则对数字进行加法操作;PUT请求用于重置数字。通过NumberWeb、NumberService和NumberOperation三个类实现业务逻辑,并使用Postman进行测试验证。
摘要由CSDN通过智能技术生成

一、HTTP基础知识

参考这里https://www.jianshu.com/p/9b8b9672b8e6

二、项目代码

1.项目的创建

可以参考SpringBoot简单入门之Helloworld

2.项目内容

通过http不同的请求来进行对数字进行相应的操作。

  1. 当发出get请求的时候对数字进行初始化为0。
  2. 当发出post请求的时候对数字进行相加操作。
  3. 当发出put请求的时候把数字重置为指定值。

3.核心代码

项目结构

在这里插入图片描述

负责处理http请求的NumberWeb类

根据不同的请求来执行不同的函数,最后返回执行结果。

package com.example.demo.controller;

import com.example.demo.operation.NumberOperation;
import com.example.demo.service.NumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class NumberWeb {
    @Autowired

    /**
     * GET请求,页面加载时执行
     */
    @ResponseBody
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String init(){
        NumberService.init();
        return "初始化为: "+NumberService.get();
    }

    /**
     * POST请求,对数字进行运算
     * @param n
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/index",method = RequestMethod.POST)
    public String get(int n){
        NumberService.add(n);
        return "新的数字: "+NumberService.get();
    }


    /**
     * PUT请求,重新设置数字的值
     * @param n
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/index",method = RequestMethod.PUT)
    public String set(int n){
        NumberService.set(n);
        return "新的数字: "+NumberService.get();
    }


}



负责对数字进行相关操作的NumberService类

主要负责对调用NumberOperation的对应方法

package com.example.demo.service;

import com.example.demo.operation.NumberOperation;

public class NumberService {
    /**
     * 把新的数字和原来数字进行相加
     * @param n
     */
    public static void add(int n){
        NumberOperation.getNumberOperation().add(n);
    }

    /**
     * 返回数字
     * @return
     */
    public static int get(){
        return NumberOperation.getNumberOperation().getNumber();
    }

    /**
     * 重新给数字赋值
     * @param n
     */
    public static void set(int n){
        NumberOperation.getNumberOperation().setNumber(n);
    }

    /**
     * 初始化数字
     */
    public static void init(){
        NumberOperation.getNumberOperation().init();
    }
}

操作NumberService的NumberOperation类

主要提供操作数字的相关方法。

package com.example.demo.operation;

public class NumberOperation {
    private int number;//数字
    private static NumberOperation numberOperation = new NumberOperation();//接口
    private NumberOperation(){

    }

    /**
     * 返回接口
     * @return
     */
    public static NumberOperation getNumberOperation() {
        return numberOperation;
    }

    /**
     * 设置新的数字
     * @param number
     */
    public void setNumber(int number) {
        this.number = number;
    }

    /**
     * 返回数字
     * @return
     */
    public int getNumber() {
        return number;
    }

    /**
     * 对数字进行相加
     * @param n
     */
    public synchronized void add(int n){
        this.number+=n;
    }

    /**
     * 初始化数字
     */
    public void init(){
        this.number=0;
    }
}

三、测试

使用postman软件进行模拟请求

1.测试get

当发出get请求的时候,可以看到数字初始化为0。
在这里插入图片描述

2.测试post

把请求改为post,同时连续请求两次,参数值分别为5和4。
在这里插入图片描述
可以发现每一次请求后n的值都增加了对应的值。
在这里插入图片描述

3.测试put

把请求改为put,同时参数改为3可以发现值也变为了3。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值