SpringBoot学习笔记(第四课时:Controller的使用)

SpringBoot学习笔记(第四课时:Controller的使用)

目录

一、@Controller、@RestController和@RequestMapping 解释
二、@PathVariable和@RequestParam解释

概要

注解解释
@Controller处理HTTP请求
@RestControllerSpring4之后新家的注解,原来返回JSON需要ResponseBody配合@Contrloler。现在只需要只一个就ok
@RequestMapping配置url映射
@PathVariable获取url中的数据
@RequestParam获取请求参数的值

一、@Controller、@RestController和@RequestMapping 解释

在我们前面的HelloController类文件中
在这里插入图片描述
@RestController改为@Controller,我们就不能使用return信息,再次启动的时候报404错误,如下
在这里插入图片描述
在这里插入图片描述
如果坚持使用@Controller这个注解的话,我们需要在pom.xml中新加一点内容

<dependencies>
......
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>
</dependencies>

然后我们重新import一下, 那如何查看下载成功了呢?windows 按住Ctrl+鼠标左键点击,
在这里插入图片描述
可以点进去说明成功了,第二步,我们在resource -> template下新建HTMLFile文件index
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>好好学习SpringBoot</h1>
</body>
</html>

然后我们在HelloController里面使用这个模板

 public String say() {
      // return "说明:" + limitConfig.getDescription();
      return "index";
 }

结果如下:
在这里插入图片描述
增加或减少依赖都需要重新import一下

如果我们需要访问/hello和/hi是一样的,我们何如?@GetMapping可以使用数组(Ctrl + P)

@GetMapping({"/hello","/hi"})

接下来我们希望访问的是/hello/say才进入say那个方法,你可以如下:

@GetMapping("/hello/say")

但是这种不利于代码的维护,我们可以如下:

// 注解
@RestController
@RequestMapping("/hello")
public class HelloController {
    @Autowired
    private LimitConfig limitConfig;
    @GetMapping("/say")
    public String say() {
        return "说明:" + limitConfig.getDescription();
    }
}

在这里插入图片描述
在这里插入图片描述
我们将上面的@GetMapping()换成@PostMapping()注解,再次访问
在这里插入图片描述
错误信息是方法不被允许,状态时405,“Get”不支持。
我们将请求链接在postman软件中使用post请求试试,我们会得到如上:
在这里插入图片描述
那有没有方法使得既支持get又支持postn呢,我们试试:
我们将@PostMapping()注解换成@RequestMapping()试试,我们再次在浏览器和postman中请求,我们会发现,此方法时可以实现的,个人不推荐这种,要么Get,要么Post

二、@PathVariable和@RequestParam解释

1、当我们要获取http://localhost:8081/luckymoney/hello/say/100这个请求中得到100这个值

// 注解
@RestController
@RequestMapping("/hello")
public class HelloController {
    @Autowired
    private LimitConfig limitConfig;
    @GetMapping("/say/{id}")
    public String say(@PathVariable("id") Integer id) {
        // return "说明:" + limitConfig.getDescription();
        return "id:" + id;
    }
}

2、当然,http://localhost:8081/luckymoney/hello/say?id=100还有这种:

// 注解
@RestController
@RequestMapping("/hello")
public class HelloController {
    @Autowired
    private LimitConfig limitConfig;
    @GetMapping("/say")
    public String say(@RequestParam("id") Integer id) {
        // return "说明:" + limitConfig.getDescription();
        return "id:" + id;
    }
}

3、那有的时候,http://localhost:8081/luckymoney/hello/say访问会报错
在这里插入图片描述
我们将id为非必传

// 注解
@RestController
@RequestMapping("/hello")
public class HelloController {
    @Autowired
    private LimitConfig limitConfig;
    @GetMapping("/say")
    // required = true|false (必传|非必传) ; defaultValue = "0" -> 默认值(赋值不能用int 只能用String)
    public String say(@RequestParam(value = "id", required = false, defaultValue = "0") Integer id) {
        // return "说明:" + limitConfig.getDescription();
        return "id:" + id;
    }
}

这是@GetMapping()方式,那么@PostMapping方式呢?

我们在postman中请求
在这里插入图片描述
带上id,postman会帮我们加了一点东西
在这里插入图片描述
post请求方式参数放在body里面(x-www-form-urlencoded请求方式更常见一些)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值