一,GET请求
1.1 请求相关注解
@RequestMapping
- 即是类注解,也是方法注解
- value/path:请求的路径
- method
- 请求的方式
- 
GET,POST
- HEAD,PUT,PATCH,DELETE,OPTIONS,TRACE
 
- 
 
- 请求的方式
@GetMapping
- 简化常用的HTTP方法的映射,并更好地表达被注解方法的语义
- 相当于@RequestMapping(method = RequestMethod.GET)
- params
- 具体到请求参数值访问该方法(params)
 
 //浏览器发送请求路径:http://localhost:8080/t/topic/{id}?sid={sidValue}
    @GetMapping(value = "/topic/{id}",params = {"id=88"})
    String getTopicId(@PathVariable int id ,  @RequestParam(defaultValue = "66") int sid){
        return "这是一个帖子地址为:"+ id + "并且参数sid为"+ sid + "的内容!" ;
    }
@PathVariable
- 获取url中的数据
- name/value
- 要绑定的请求参数的名称,跟URI上填写的路径名一样
 
- 要绑定的请求参数的名称,跟
- required
- 含义:请求参数是否必填
- 默认true
- 请求缺少参数会引发异常
- 请求中不存在参数或参数为null- 1)required声明为false
 
- 1)
 
- 请求中不存在参数或参数为
 
//浏览器发送请求路径:http://localhost:8080/t/top/{id}?sid={sidValue}
    //`required`声明为`false`的情况:请求参数值为非必填,地址是不一样的,需要配置多个地址映射
    @GetMapping(value = {"/top/{id}/u", "/top/u"})
    String getTopU(@PathVariable(value="id",required = false) String topid ,  @RequestParam(defaultValue = "66") int sid){
        return "这是一个帖子地址为:"+ topid + "并且参数sid为"+ sid + "的内容!" ;
    }
@RequestParam
- 获取请求参数的值
- name/value
- 要绑定的请求参数的名称,跟URL上一样
 
- 要绑定的请求参数的名称,跟
- defaultValue
- 请求参数未提供或空时的默认值
- 提供默认值会隐式地将 required设置为false
 
- required
- 含义:请求参数是否必填
- 默认true
- 请求缺少参数会引发异常
- 请求中不存在参数或参数为null- 1)required声明为false
- 2)提供defaultValue
 
- 1)
 
- 请求中不存在参数或参数为
 
1.2 无参GET请求 - 基本实现步骤
- 创建与前端请求交互的包 controller
- 在包下创建与first请求交互的类
- 在 类上添注解RestController说明这是一个和web请求交互的类
- 在类内编写请求方法
- 在方法上添加GET请求注解及其请求路径
- 启动服务,浏览器输入验证
实现方式:
- 
@GetMapping注解
- 
@RequestMapping注解
//    @GetMapping("/first")
    @RequestMapping(path="/first", method = RequestMethod.GET)
    String first(){
        return "hello spring boot !" ;
    }
1.3 url带可变参数的请求 - 基本实现步骤
- 创建与前端请求交互的包 controller
- 在包下创建与请求交互的类
- 在类上添注解RestController
- 在类内编写请求方法:
- 方法需要有参数
- 方法的参数要和前端请求声明参数名绑定
- 
id作为请求URI后的参数,请求路径需要有该参数
- 在方法上添加GET请求注解及其请求路径
- 启动服务,浏览器输入验证
 //@PathVariable:URL路径参数注解,绑定id和sid
    @GetMapping("/topic/{id}")
    String getTopic(@PathVariable("id") int  tid){
        return "这是一个帖子为:"+ tid + "的内容" ;
    }
    //如果前端请求参数和方法参数名一致则省略声明
    @GetMapping("/topic/{id}")
    String getTopicId(@PathVariable int  id){
        return "这是一个帖子为:"+ id + "的内容" ;
    }
1.4 param带可变参数的请求 - 基本实现步骤
- 创建与前端请求交互的包 controller
- 在包下创建与请求交互的类
- 在类上添注解RestController说明这是一个和web请求交互的类
- 在类内编写请求方法
- 方法需要有sid参数
- 方法的 sid参数要和前端请求声明参数名绑定
- 
sid参数作为拼接参数,请求路径没有该参数
- 在方法上添加GET请求注解及其请求路径
- 启动服务,浏览器输入验证
//queryParam参数:sid
    //RequestParam 注解:queryParam参数注解,绑定sid和id
    @GetMapping("/native")
    String getNative(@RequestParam("sid") String id){
        return "这是一个本国的地址为:"+ id + "的内容!" ;
    }
    //如果前端请求参数和方法参数名一致则省略声明
   //浏览器请求地址:http://localhost:8080/native?sid={sid}
    @GetMapping("/native")
    String getSid(@RequestParam String sid){
        return "这是一个本国的地址为:"+ sid + "的内容!" ;
    }
1.5 带混合参数的请求 - 基本实现步骤
- 创建与前端请求交互的包 controller
- 在包下创建与请求交互的类
- 在类上添注解RestController说明这是一个和web请求交互的类
- 在类内编写请求方法
- 方法需要有id参数及sid参数
- 方法的参数要和前端请求声明参数名绑定
- 
id参数作为URI路径参数,请求路径需要写上该参数
- 
sid参数作为拼接参数,请求路径没有该参数
- 在方法上添加GET请求注解及其请求路径
- 启动服务,浏览器输入验证
 //浏览器发送请求路径:http://localhost:8080/t/topic/{id}?sid={sidValue}
    @GetMapping("/t/topic/{id}")
    String getTopicId(@PathVariable int id ,  @RequestParam int sid){
        return "这是一个帖子地址为:"+ id + "并且参数sid为"+ sid + "的内容!" ;
    }
- 浏览器页面验证正常:
 
1.6 带默认值的请求
- 只有@RequestParam注解才会有defaultValue 默认值
//浏览器发送请求路径:http://localhost:8080/t/topic/{id}?sid={sidValue}
    @GetMapping("/t/topic/{id}")
    String getTopicId(@PathVariable int id ,  @RequestParam(defaultValue = "66") int sid){
        return "这是一个帖子地址为:"+ id + "并且参数sid为"+ sid + "的内容!" ;
    }
1.7 多个参数的请求
//浏览器发送请求路径:http://localhost:8080/t/top/{city}/{year}?describe=&money=
    @GetMapping("/t/top/{city}/{year}")
    String getCityAndYear(@PathVariable int year , @PathVariable String city,
                          @RequestParam(defaultValue = "支出") String describe ,@RequestParam(defaultValue = "45678") long money){
        return  year + "年"+ city + "人均"+ describe + "为:" +money + "元!";
    }
- 浏览器页面验证正常:
 
1.8 提取共同的请求路径
- 使用类注解 @RequestMapping添加请求的统一路径
- 请求统一路径添加必须是开头
- 不能是 中间 和 结尾
- 同一类中的接口路径不能重复,否则会报错
 
package com.ceshiren.springtest.controller;
import org.springframework.web.bind.annotation.*;
@RestController
//RequestMapping在类上的注解,表示提取请求的共同路径
@RequestMapping("/t")
public class FirstController {
    //浏览器发送请求路径:http://localhost:8080/t/topic/{id}?sid={sidValue}
    @GetMapping("/topic/{id}")
    String getTopicId(@PathVariable int id ,  @RequestParam(defaultValue = "66") int sid){
        return "这是一个帖子地址为:"+ id + "并且参数sid为"+ sid + "的内容!" ;
    }
    //浏览器发送请求路径:http://localhost:8080/t/top/{city}/{year}?describe=&money=
    @GetMapping("/top/{city}/{year}")
    String getCityAndYear(@PathVariable int year , @PathVariable String city,
                          @RequestParam(defaultValue = "支出") String describe ,@RequestParam(defaultValue = "45678") long money){
        return  year + "年"+ city + "人均"+ describe + "为:" +money + "元!";
    }
}

