POM记得加<parent部分,这样不容易出错?
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.6.13</version>
<relativePath/>
</parent>
第一个controller记得在开头加上@RESRCONTEROLLER
这里@restcontroller 只能是view请求数据
默认情况下@Rescontroller返回数据json格式
@Controller 可以是请求数据和页面 (在03讲了)
添加依赖时,注意别添加到管理包中,不然maven自动更新不会起作用
<dependencyManagement>
@RequestMapping注解主要负责URL路由映射,可以添加到Controller或者具体方法
常见参数
value:URL路径 支持通配符? 和**
method http请求方法 @RequestMapping(value =" /getData",method = RequestMethod.GET)
consumes 请求的媒体类型 (content-type) 例如json
produces
params,headers
String getTest3(@RequestParam(value = "nickname") String name ))
这里用一个注解把原本参数重新命名,这个方法非常常用例如用户传参是nickname = 123,我们这里接受后,重新命名为name,后面可以用name这个参数
public String postTest4(@RequestBody User user){
有时候参数是请求体,这个也会常用