SpringBoot学习笔记(第四课时:Controller的使用)
目录
一、@Controller、@RestController和@RequestMapping 解释
二、@PathVariable和@RequestParam解释
概要
| 注解 | 解释 |
|---|---|
| @Controller | 处理HTTP请求 |
| @RestController | Spring4之后新家的注解,原来返回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请求方式更常见一些)

本文详细介绍了SpringBoot中@Controller、@RestController和@RequestMapping等注解的使用,包括它们的区别及如何处理HTTP请求。同时,深入探讨了@PathVariable和@RequestParam注解在获取URL路径参数和请求参数值的应用场景。
1684

被折叠的 条评论
为什么被折叠?



