SpringBoot 学习三:Controller的使用

1、给同一个类添加两个访问地址

在浏览器里输入: localhost:8081/hello 或者 localhost:8081/hi都能访问到。

SpringBoot 学习三:Controller的使用

2、给整个类指定一个URL 

通过设置@RequestMapping("/hello"),给整个类指定一个URL 

SpringBoot 学习三:Controller的使用

这个时候就需要通过http://localhost:8081/hello/hi去访问这个类了。

3、如何处理url中的参数

SpringBoot 学习三:Controller的使用

(1) 使用@PathVariable获取url中的参数

在浏览器中输入 http://localhost:8081/hello/100/say

SpringBoot 学习三:Controller的使用

 

2、使用@RequestParam获取参数

SpringBoot 学习三:Controller的使用

在浏览器中输入:http://localhost:8081/hello/say?id=100

设置参数的默认值

SpringBoot 学习三:Controller的使用