软件园学生在线

  • {{ item.name }}
  • 2023试用期

登录与注册

【后端一】 闫飞

  • 闫飞
  • 2022-10-14
  • 0

基于Spring boot的四则运算API

一 产品说明

实现实数范围内的四则运算 (double范围内)

二 使用说明

通过URL

运算 路径
加法 localhost:8082/calc/add?n1=%s&n2=%s
减法 localhost:8082/calc/subtraction?n1=%s&n2=%s
乘法 localhost:8082/calc/multiply?n1=%s&n2=%s
除法 localhost:8082/calc/divide?n1=%s&n2=%s
  • %s为你想输入的数字

三 考虑的情况

  1. 通过将int类型改为float类型实现了小数运算
  2. 在除法除数为零时会输出Infinity

四 学习经历

1 文档学习

@RestController = @Controller + @ResponseBody

@Controller 将当前修饰的类注入SpringBoot IOC容器,使得从该类所在的项目跑起来的过程中,这个类就被实例化。当然也有语义化的作用,即代表该类是充当Controller的作用

@ResponseBody 它的作用简短截说就是指该类中所有的API接口返回的数据,甭管你对应的方法返回Map或是其他Object,它会以Json字符串的形式返回给客户端

@GetMapping用于处理请求方法的GET类型

2 自主学习

  1. 尝试制作一个前端,但很遗憾没有成功,不过学习了一些HTML语法
  2. 尝试用参数检验输入不为空,但也没有成功,不过学习了如何使用@Validated和@Valid
  3. 发现一些关键字和注解删除后依然可以运行,如@RequestParam static 学习了他们的用途,但是还是不太明白
  4. 成功破解idea

五 代码

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
    @GetMapping("/add")
    public double add(@RequestParam double n1 , @RequestParam double n2){
        return n1 + n2 ;
    }
    @GetMapping("/subtraction")
    public double subtraction( @RequestParam double n1 , @RequestParam double n2){
           return n1 - n2;
    }
    @GetMapping("/multiply")
    public double multiply( @RequestParam double n1 , @RequestParam double n2){
        return n1 * n2 ;
    }
    @GetMapping("/divide")
    public double divide( @RequestParam double n1 , @RequestParam double n2){

        return n1 / n2 ;
    }
}

技术力十分低下的作品,有太多的想法,但是限于自己的能力,无法实现。要学习的东西还有很多很多。

应该说,想要实现这个作业的基础功能是十分容易的事情,但是想要增加功能就超出了我的能力范围。

Springboot的难度很大,网上的教程基本看不懂,但路漫漫其修远兮吾将上下而求索

我有信心将来有一天能掌握这些技术

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Calculation</title>
</head>
<body>
    <h2>四则运算计算器</h2>
            <form action="/calc">
            <label>
                第一个数字:
                <input type="number" name="n1">
            </label><br>
            <label>
                第二个数字:
                <input type="number" name="n2">
            </label><br>
                <button type="button">+</button>
                <button type="button">-</button>
                <button type="button">*</button>
                <button type="button">/</button>
                <input type="submit" value="取得结果">
                <h3 th:text="${data1}"></h3>
                <h3 th:text="${data2}"></h3>
            运算结果:
        </form>

</body>
</html>

完全没有的用的前端页面

闫飞
闫飞
© 2025 软件园学生在线
Theme by Wing