博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_8、SpringBoot基础HTTP接口GET请求实战...
阅读量:4325 次
发布时间:2019-06-06

本文共 1309 字,大约阅读时间需要 4 分钟。

3、SpringBoot基础HTTP接口GET请求实战
    简介:讲解springboot接口,http的get请求,各个注解使用
        1、GET请求
            1、单一参数@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测试

@GetMapping

简化,直接定义方位get请求,使用@GetMapping。是Spring Boot给我们提供的注解

点进去@GetMapping实际上就包装了一层@RequestMapping里面默认设置了method是get
故意用post方式请求

@RequestParam

参数的默认值。

name是别名。接口要传递的参数为page=110
required表示字段是必须的
不传page

@RequestBody

相应的数据

@RequestHeader

断点调试

HttpServletRequest

 

转载于:https://www.cnblogs.com/wangjunwei/p/11393680.html

你可能感兴趣的文章
搜索框键盘抬起事件2
查看>>
阿里百川SDK初始化失败 错误码是203
查看>>
透析Java本质-谁创建了对象,this是什么
查看>>
BFS和DFS的java实现
查看>>
第四次博客作业
查看>>
C++ GUID和string转化函数【转载】
查看>>
save与persist差别
查看>>
采用传输矩阵法求解光子晶体带隙
查看>>
nginx 生产中配置记录
查看>>
css背景渐变色
查看>>
Coursera machine learning 第二周 quiz 答案 Linear Regression with Multiple Variables
查看>>
可视化与办公自动化学习
查看>>
关于jquery中prev()和next()的用法
查看>>
for循环的类型以及while循环
查看>>
enote笔记语言(2)(ver0.2)
查看>>
Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1
查看>>
seajs的使用--主要了解模块化
查看>>
Noip2018游记
查看>>
一、 kettle开发、上线常见问题以及防错规范步骤
查看>>
eclipse没有server选项
查看>>