SpringMVC常用注解


前言

这是一位帅气的小帅哥,在学习SpringBoot中学习笔记,希望能够帮到大家。

1. Controller

@Controller
处理http请求

2. RestController

是 @Controller 与@ResponseBody 的组合注解
如果一个 Controller 类添加了@RestController,那么该 Controller 类下的所有方法都相当于添加了@ResponseBody 注解
用于返回字符串或 json 数据

//@Controller
@RestController //相当于控制层类上添加@Controller + 方法上加上@ResponseBody,
// 意味着当前控制层类中所有方法返回的都是json对象
public class StudentController {
    @RequestMapping(value = "/student")
    public /*@ResponseBody*/Object student(){
        Student student = new Student();
        student.setId(1001);
        student.setName("zhangsan");
        return student;
    }

运行结果,返回的是JSON对象
运行结果

3. RequestMapping(常用)

支持GET和POST请求
	@RequestMapping(value = "queryStudentById2", method = RequestMethod.GET)
//该注解通常在查询数据时使用 -> 查询
	public Object queryStudentById2(){
        return "Ony GET Method";
    }

	@RequestMapping(value = "/insert",method = RequestMethod.POST)
    //该注解通常在新增数据的时候使用 -> 新增
    public Object insert(){
        return "Insert success";
    }

    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
//    该注解通常在删除时使用 ->删除
    public Object delete(){
        return "delete Student";
    }

    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    //通常用在更新数据的时候。 ->更新
    public Object update(){
        return "update student info";
    }

此时出GET请求外,我们无法使用浏览器进行测试。
我们可以通过HTTP接口请求工具Postman进行测试
在这里插入图片描述

4. GetMapping

相当于
@RequestMapping(value = "queryStudentById2", method = RequestMethod.GET)

5. PostMapping

相当于

@RequestMapping(value = "/insert",method = RequestMethod.POST)

6. DeleteMapping

相当于

@RequestMapping(value = "/delete",method = RequestMethod.DELETE)

7. PutMapping

相当于

@RequestMapping(value = "/update",method = RequestMethod.PUT)

总结

学习的路还很漫长,兄弟们,坚持住啊!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值