SpringBoot集成SpringMVC

1. SpringMVC常见注解

@RequestMapping的请求方式

  1. 如果方法上的@RequestMapping注解没有设置method属性,则请求方式支持:GET和POST请求
  2. 如果方法上的@RequestMapping注解设置了method属性,则只能是相应的请求方式可以访问。

@Controller
@ResponseBody
@RestController
@RequestMapping
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping

1. 1 @RestController

@RestController = @Controller+@ResponseBody
所有方法都是JSON对象,控制层的注解可以换成@RestController
相当于控制层类上加上@Controller + 方法上加@ResponseBody
意味着当前控制层类中所有方法返回值都是JSON对象

1. 2 @GetMapping

@GetMapping(value=“/query”) = @RequestMapping(value=“/query”,method = RequestMethod.GET)
只接收GET请求,请求方式不对就报405错误
该注解通常在查询数据的时候使用 ----->查询

1. 3 @PostMapping

@PostMapping(value=“/insert”) = @RequestMapping(value=“/insert”,method = RequestMethod.POST)
该注解通常在新增数据的时候使用 ----->新增

1. 4 @DeleteMapping

@DeleteMapping(value=“/delete”) = @RequestMapping(value=“/delete”,method = RequestMethod.DELETE)
该注解通常在删除数据的时候使用 ----->删除

1. 5 @PutMapping

@PutMapping(value=“/update”) = @RequestMapping(value=“/update”,method = RequestMethod.PUT)
该注解通常在修改数据的时候使用 ----->更新

2. SpringBoot实现RESTful

2.1 认识RESTful

RESTful:是一种互联网软件架构设计的风格
它只是提出了一组客户端和服务端交互时的架构理念和设计原则,基于这种理念和原则设计的接口可以更简洁,更有层次。

任何的技术都可以实现这种理念,如果一个架构符合REST原则,就称它为RESTful架构。

比如:
我们要访问一个http接口:http://localhost:8080/boot/order?id=1001&status=1
采用RESTful风格则http地址为:http://localhost:8080/boot/order/1001/1

2.2 SpringBoot开发RESTful主要注解

2.2.1 @PathVariable

@PathVariable
获取url中的数据
该注解是实现RESTful最主要的一个注解

2.2.2 @PostMapping

@PostMapping
接收和处理Post方式的请求

 @PostMapping("/student/{id}/{name}")   //RESTful风格
    public Object student(@PathVariable("id") Integer id,
                          @PathVariable("name") String name){
        HashMap<String,Object> restMap = new HashMap<>();
        restMap.put("id",id);
        restMap.put("name",name);
        return restMap;
    }

注意:请求路径冲突问题

1.通常在RESTful风格方法的请求方式会按增删改查的请求方式来区分
2.修改请求路径
3.RESTful请求风格要求路径中使用的单词都是名词,最好不要出现动词
增POST请求
删DELETE请求
改PUT请求
查GET请求

如果传的参数不是数据库表中的字段可以不采用斜杠

3.测试

请求地址相同,通过请求方式不同来区分请求

@RestController
public class RestfulController {
    @GetMapping("/rest")
    public Object rest(Integer id ,String name){
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        return student;
    }
    @GetMapping("/student/{id}/{name}")   //RESTful风格
    public Object student(@PathVariable("id") Integer id,
                          @PathVariable("name") String name){
        HashMap<String,Object> restMap = new HashMap<>();
        restMap.put("id",id);
        restMap.put("name",name);
        return restMap;
    }
    @PostMapping("/student/{id}/{age}")
    public Object student2(@PathVariable("id") Integer id,
                          @PathVariable("age") Integer age){
        HashMap<String,Object> restMap = new HashMap<>();
        restMap.put("id",id);
        restMap.put("age",age);
        return restMap;
    }
}

GET方式请求
在这里插入图片描述POST方式请求
在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

848698119

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值