后端第一次培训-基于Spring Boot的Rest Api
一. 产品说明
1.使用方法:在地址栏输入localhost:8080/plus(或minus或multiply或divide,根据你想实现的运算来选择)?a=3&b=4(数字自己定);
2.特殊情况考虑:
1)当进行除法运算时,整型无法满足计算需求,所以应把数据类型定为double;
2)除法运算中分母不能为零,所以需要一个if语句来判断使用者输入的数据中分母是否为零:当分母为零时,输出语句为字符串,所以应该将public int divide改为public String divide,经此一改,那么当分母不为零时,return a/b;将不可行,所以把这里的返回值也改成字符串return(“a/b=”+a/b)。经过以上两个修改点,即可顺利完成除法运算。
二.学习历程
1.文档学习
1)文档中出现了三个注解
- @RestController注解:是@Controller注解与@ResponseBody注解的结合
 - @GetMapping注解:
 - @RequestParam注解:请求参数
 
2)public String hello(Requestparam int a){}
第二个String表示返回值的类型
第三个是自己起的名字
@Requestparam int a是请求一个整形参数a
花括号内是执行的语句和返回值
2.拓展学习
html
<html>
    <head>
        <title>hh</title>
    </head>
    <body>
        <form>
            <input type="text"/ >
            <select>
                <option>+</option>
                <option>-</option>
                <option>×</option>
                <option>÷</option>
            </select>
            <input type="text">
            =
            <input type="text">
        </form>
    </body>
</html>
虽然但是,会写前端但不会前后端交互...
三.代码
package com.example.houduan2;
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("/plus")
    public String plus(@RequestParam int a,@RequestParam int b){
       return("a+b="+(a+b));
    }
    @GetMapping("/minus")
        public String minus(@RequestParam int a,@RequestParam int b){
        return("a-b="+(a-b));
    }
    @GetMapping("/multiply")
    public String multiply(@RequestParam int a,@RequestParam int b){
        return("a*b="+(a*b));
    }
    @GetMapping("/divide")
    public String divide(@RequestParam double a,@RequestParam double b){
        if(b!=0)
        return("a/b="+(a/b));
        else
           return("做除法运算时,分母不能为0,请重新输入");
    }
    }