三、SpringBoot基础HTTP接口GET请求实战
简介:讲解springboot接口,http的get请求,各个注解使用
一、GET请求
一、单一参数@RequestMapping(path = "/{id}", method = RequestMethod.GET)
1) public String getUser(@PathVariable String id ) {}
2)@RequestMapping(path = "/{depid}/{userid}", method = RequestMethod.GET) 能够同时指定多个提交方法
getUser(@PathVariable("depid") String departmentID,@PathVariable("userid") String userid)
3)一个顶俩
@GetMapping = @RequestMapping(method = RequestMethod.GET)
@PostMapping = @RequestMapping(method = RequestMethod.POST)
@PutMapping = @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)
4)@RequestParam(value = "name", required = true)
能够设置默认值,好比分页
4)@RequestBody 请求体映射实体类
须要指定http头为 content-type为application/json charset=utf-8
5)@RequestHeader 请求头,好比鉴权
@RequestHeader("access_token") String accessToken
6)HttpServletRequest request自动注入获取参数前端
get请求注解比较多因此单独来说
新建一个getController
使用注解RestController表示返回json给前端
接口统一用小写字母
使用@PathVariable获取的city_id赋值给userId
使用postman测试
spring
简化,直接定义方位get请求,使用@GetMapping。是Spring Boot给咱们提供的注解
点进去@GetMapping实际上就包装了一层@RequestMapping里面默认设置了method是get
故意用post方式请求
json
参数的默认值。
name是别名。接口要传递的参数为page=110
required表示字段是必须的
不传page
springboot
相应的数据
app