软件园学生在线

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

登录与注册

后端一 吕明翰

  • LMH
  • 2023-10-29
  • 0

后端第一次作业

基本结构

首先按照课上内容创建control文件和service文件,但是这里需要我们写加减乘除四个运算,所以我创建了四个service文件,分别与之对应。搜索过关于四个方法可不可以写在一个service中,但是没有实现,而且害怕会出现没有想到的bug所以暂时不进行这个操作。
control,以加法运算为例:

package com.example.demo.controller.Main;

import com.example.demo.service.Mainservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;

@RestController
public class control {

    @Autowired
    Mainservice mainService;

    @GetMapping("/add")
    public Double add(@RequestParam Double a, @RequestParam Double b) {
        return mainService.add(a, b);
    }
}

service中,同样以加法为例:

package com.example.demo.service;

import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Service
public class Mainservice {
    @GetMapping
    public Double add(@RequestParam Double a, @RequestParam Double b) {

        return a + b;
    }
}

实现识别数据类型

这里消耗了最多时间,尝试了很多方法·。
首先我想到了注释里有没有相关的注解可以实现这个内容,搜索过后发现只有判断正负如@PositiveOrZero和@NegativeOrZero,数据精度,输入是否为空或者输入内容是不是邮箱,生日等等。在最后我发现了一个方法@Validated可以用来判断输出的数据。所以使用了这个注释,但是很可惜在postman里输出的是“error":Badrequest,这明显不符合要求。
在这之后我开始找java自带的方法,书上的double nextDouble和我找到的hasNextDouble但是经过尝试没有找到这俩个方法怎么在Springboot中使用。
下一个尝试的方法是instanceof

package com.example.demo.service;

import org.springframework.stereotype.Service;
@Service
public class Secondservice {
    public Double minus( Double a,  Double b) {
        if (a instanceof Double) {
            if (b instanceof Double) {
                return a - b;
            } else
            {return b;}
        }else
        {return a;}
    }
}

这次没有报错但是运行之后postman给出的结果是”error“:not found明显这也不是想要的结果。检查后发现idea告诉我”a"和“b”一直为null。产找资料后发现a和b本来就是Double,所以再次检查不符合需求。所以改变输入数据类型并用了一个新的方法后结果是这样的,以乘法为例:

package com.example.demo.service;

import org.springframework.stereotype.Service;

@Service
public class Thirdservice {
public Double multiply(String a, String b) {
if (isDouble(a) && isDouble(b)) {
    double num1 = Double.parseDouble(a);
    double num2 = Double.parseDouble(b);
       return num1 * num2;
    } else {try {
         throw new IllegalArgumentException("Invalid input. Both 'a' and 'b' must be of type Double.");
         }catch(IllegalArgumentException e) {
         System.out.println("Data must be Double" + e.getMessage());
         return null;
        }
    }
}
private boolean isDouble(String str) {
    try {
       Double.parseDouble(str);
         return true;
       } catch (NumberFormatException e) {
         return false;
       }
    }
}

这次程序成功的在输入了不符合要求的数据后返回了一个空白页。

解决数据溢出

一开始打算直接用Doouble来写这样数据就会以科学计数法的形式输出,但是看到群里的内容后决定改用BigDecimal来解决这个问题。
依然以除法为例子:

package com.example.demo.service;

import org.springframework.stereotype.Service;
import java.math.BigDecimal; // 引入
@Service
public class Thirdservice {
    public BigDecimal multiply(String a, String b) {
        if (isBigDecimal(a) && isBigDecimal(b)) {
            BigDecimal num1 = new BigDecimal(a);
            BigDecimal num2 = new BigDecimal(b); // 判断两个输入数据类型
            return num1.multiply(num2); //进行计算
        } else {try {
            throw new IllegalArgumentException("Invalid input. Both 'a' and 'b' must be of type Double.");
        }catch(IllegalArgumentException e) {
            System.out.println("Data must be Double" + e.getMessage());
            return null; //若输入错误则返回空页面
        }
        }
    }
    private boolean isBigDecimal(String str) {
        try {
            new BigDecimal(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    } /* 这段代码的作用是判断给定的字符串是否可以转换为BigDecimal类型。
         它尝试使用BigDecimal类的构造函数将字符串转换为BigDecimal对象,如果转换成功,则返回true;
         如果转换失败,抛出NumberFormatException异常,则返回false。
         通过这个方法,可以检查一个字符串是否符合BigDecimal的格式要求,例如是否包含有效的数字和小数点。*/
}

在postman中输入localhost:8080/ ?a= &b= ,分别选择输入add,subtract,multiply,divide分别对应加减乘除,之后对a,b赋值。如果输入内容不正确则返回空页面。

controll

package com.example.demo.controller.Main;

import com.example.demo.service.Mainservice;
import com.example.demo.service.Secondservice;
import com.example.demo.service.Thirdservice;
import com.example.demo.service.Fourthservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;

@RestController
public class control {

    @Autowired
    Mainservice mainService;
    @Autowired
    Secondservice secondService;
    @Autowired
    Thirdservice thirdService;
    @Autowired
    Fourthservice fourthService;

    @GetMapping("/add")
    public BigDecimal add(@RequestParam("a") String a, @RequestParam("b") String b) {
        return mainService.add(a, b);
    }

    @GetMapping("/subtract")
    public BigDecimal subtract(@RequestParam("a") String a, @RequestParam("b") String b) {
        return secondService.subtract(a, b);
        }

    @GetMapping("/multiply")
    public BigDecimal multiply(@RequestParam("a") String a, @RequestParam("b") String b) {
        return thirdService.multiply(a, b);
    }

    @GetMapping("/divide")
    public BigDecimal remainder(@RequestParam("a") String a, @RequestParam("b") String b) {
        return fourthService.divide(a, b);
    }
}

Mainservice

package com.example.demo.service;

import org.springframework.stereotype.Service;
import java.math.BigDecimal; // 引入
@Service
public class Mainservice {
    public BigDecimal add(String a, String b) {
        if (isBigDecimal(a) && isBigDecimal(b)) {
            BigDecimal num1 = new BigDecimal(a);
            BigDecimal num2 = new BigDecimal(b); // 判断两个输入数据类型
            return num1.add(num2); //进行计算
        } else {try {
            throw new IllegalArgumentException("Invalid input. Both 'a' and 'b' must be of type Double.");
        }catch(IllegalArgumentException e) {
            System.out.println("Data must be Double" + e.getMessage());
            return null; //若输入错误则返回空页面
        }
        }
    }
    private boolean isBigDecimal(String str) {
        try {
            new BigDecimal(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}

Secondservice

package com.example.demo.service;

import org.springframework.stereotype.Service;
import java.math.BigDecimal; // 引入
@Service
public class Secondservice {
    public BigDecimal subtract (String a, String b) {
        if (isBigDecimal(a) && isBigDecimal(b)) {
            BigDecimal num1 = new BigDecimal(a);
            BigDecimal num2 = new BigDecimal(b); // 判断两个输入数据类型
            return num1.subtract(num2); //进行计算
        } else {try {
            throw new IllegalArgumentException("Invalid input. Both 'a' and 'b' must be of type Double.");
        }catch(IllegalArgumentException e) {
            System.out.println("Data must be Double" + e.getMessage());
            return null; //若输入错误则返回空页面
        }
        }
    }
    private boolean isBigDecimal(String str) {
        try {
            new BigDecimal(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}

Thirdservice

package com.example.demo.service;

import org.springframework.stereotype.Service;
import java.math.BigDecimal; // 引入
@Service
public class Thirdservice {
    public BigDecimal multiply(String a, String b) {
        if (isBigDecimal(a) && isBigDecimal(b)) {
            BigDecimal num1 = new BigDecimal(a);
            BigDecimal num2 = new BigDecimal(b); // 判断两个输入数据类型
            return num1.multiply(num2); //进行计算
        } else {try {
            throw new IllegalArgumentException("Invalid input. Both 'a' and 'b' must be of type Double.");
        }catch(IllegalArgumentException e) {
            System.out.println("Data must be Double" + e.getMessage());
            return null; //若输入错误则返回空页面
        }
        }
    }
    private boolean isBigDecimal(String str) {
        try {
            new BigDecimal(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    } /* 这段代码的作用是判断给定的字符串是否可以转换为BigDecimal类型。
         它尝试使用BigDecimal类的构造函数将字符串转换为BigDecimal对象,如果转换成功,则返回true;
         如果转换失败,抛出NumberFormatException异常,则返回false。
         通过这个方法,可以检查一个字符串是否符合BigDecimal的格式要求,例如是否包含有效的数字和小数点。*/
}

Fourthservice

package com.example.demo.service;

import org.springframework.stereotype.Service;
import java.math.BigDecimal; // 引入
import java.math.RoundingMode;

@Service
public class Fourthservice {
    public BigDecimal divide (String a, String b) {
        if (isBigDecimal(a) && isBigDecimal(b)) {
            BigDecimal num1 = new BigDecimal(a);
            BigDecimal num2 = new BigDecimal(b); // 判断两个输入数据类型
            return num1.divide(num2,RoundingMode.DOWN ); //进行计算
        } else {try {
            throw new IllegalArgumentException("Invalid input. Both 'a' and 'b' must be of type Double.");
        }catch(IllegalArgumentException e) {
            System.out.println("Data must be Double" + e.getMessage());
            return null; //若输入错误则返回空页面
        }
        }
    }
    private boolean isBigDecimal(String str) {
        try {
            new BigDecimal(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}

html

下面我们要给程序添加一个前端页面,这里我在mdn web docs里查找方法,为了实现这个要求我在resources中的templates文件夹下新建一个html项目。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>caculate</title>
</head>
<body>
<h2>加法</h2>
<form action="http://localhost:8080/add" method="get">
    <input type="number" name="a" placeholder="input a" step="any"><br>
    <input type="number" name="b" placeholder="input b" step="any"><br>
    <input type="submit" value="提交">
</form>
<h2>减法</h2>
<form action="http://localhost:8080/subtract" method="get">
    <input type="number" name="a" placeholder="input a" step="any"><br>
    <input type="number" name="b" placeholder="input b" step="any"><br>
    <input type="submit" value="提交">
</form>
<h2>乘法</h2>
<form action="http://localhost:8080/multiply" method="get">
    <input type="number" name="a" placeholder="input a" step="any"><br>
    <input type="number" name="b" placeholder="input b" step="any"><br>
    <input type="submit" value="提交">
</form>
<h2>除法</h2>
<form action="http://localhost:8080/divide" method="get">
    <input type="number" name="a" placeholder="input a" step="any"><br>
    <input type="number" name="b" placeholder="input b" step="any"><br>
    <input type="submit" value="提交">
</form>
</body>
</html>

这样就可以输入小数进行计算并且在输入的不为数字时弹出“请输入数字”的弹窗。

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