网站首页 > 教程文章 正文
1. 介绍
1.1 介绍
福哥今天把SpringBoot框架实现RESTFul接口的几个请求方式GET、POST、PUT、DELETE的接收参数的方法,SpringBoot处理HTTP请求的query参数和post参数以及raw参数基本用不到Servlet那一套,通过几个内建的注解就全部搞定了~~
2. Query和Post
在Java里面把Query参数和Post参数给混合到一起了,简单说就是你在Query里面设置了一个id,在Post里面也设置了一个id,通过Java获取id这个参数会得到Post参数。
也就是说,如果只有Query参数,那么获取的就是Query参数,如果Query和Post有同名参数,那么获取的就是Post参数,这个特性迫使我们在设计功能的时候就不能在Query和Post里面设计同名参数不同用途。
2.1 @RequestParam
根据Java的这个特性,SpringBoot设计了一个@RequestParam注解,这个注解可以用来获取Query或者Post参数,也是Post优先的规则。
2.2 @RequestBody
@RequestBody里面包括了POST的原始数据,但是你缺不能通过它去获取formData数据或者x-www-form-urlencoded数据。
但是你可以通过它去获取RAW数据,然后自己拿去做二次处理,例如:把它当作一个JSON数据进行解析。
3. GET
首先,我们来一个GET方式,GET方式只有Query参数,获取Query参数比较简单,直接定义方法参数即可,方法参数的名称就是Query的参数名称。
GET方式通@GetMapping来指明。
@GetMapping("/net.tongfu.restful.get")
public Map<String, Object> methodGet(
String queryParam1,
Integer queryParam2
){
Map<String, Object> map = new LinkedHashMap<>();
map.put("queryParam1", queryParam1);
map.put("queryParam2", queryParam2);
return map;
}
4. POST
接着我们再来一个POST方式,前面说过了,Java对Query和Post不敏感,所以我们在处理POST请求的时候要把Query参数和Post参数的名称区分开。
POST方式通@PostMapping来指明。
@PostMapping("/net.tongfu.restful.post")
public Map<String, Object> methodPost(
String queryParam1,
Integer queryParam2,
@RequestParam String postParam1,
@RequestParam Integer postParam2
){
Map<String, Object> map = new LinkedHashMap<>();
map.put("queryParam1", queryParam1);
map.put("queryParam2", queryParam2);
map.put("postParam1", postParam1);
map.put("postParam2", postParam2);
return map;
}
5. PUT
再来我们测试PUT方式,PUT方式和POST方式基本是一样的。
PUT方式通@PutMapping来指明。
@PutMapping("/net.tongfu.restful.put")
public Map<String, Object> methodPut(
String queryParam1,
Integer queryParam2,
@RequestParam String postParam1,
@RequestParam Integer postParam2
){
Map<String, Object> map = new LinkedHashMap<>();
map.put("queryParam1", queryParam1);
map.put("queryParam2", queryParam2);
map.put("postParam1", postParam1);
map.put("postParam2", postParam2);
return map;
}
6. DELETE
最后我们测试DELETE方式,DELETE方式和GET方式基本是一样的。
DELETE方式通@DeleteMapping来指明。
@DeleteMapping("/net.tongfu.restful.delete")
public Map<String, Object> methodDelete(
String queryParam1,
Integer queryParam2
){
Map<String, Object> map = new LinkedHashMap<>();
map.put("queryParam1", queryParam1);
map.put("queryParam2", queryParam2);
return map;
}
7. 默认Default
默认情况下,如果我们获取一个参数,而这个参数客户端没有提供,就会得到null值。
但是这个null值不是我们期望的,为了避免这种情况,要不强制要求客户端提供,要不就给它一个默认值。
@RequestParam可以在定义的时候通过default设置一个默认值,当客户端没有提供参数的时候就返回这个默认值。
@PostMapping("/net.tongfu.restful.default")
public Map<String, Object> methodDefault(
String queryParam1,
Integer queryParam2,
@RequestParam(defaultValue = "默认") String postParam1
){
Map<String, Object> map = new LinkedHashMap<>();
map.put("queryParam1", queryParam1);
map.put("queryParam2", queryParam2);
map.put("postParam1", postParam1);
return map;
}
8. 必选Require
默认情况下,通过@RequestParam指定的参数,如果客户端不提供这个参数的话,会报出400错误。
如果我们不要求客户端必须提供某个参数的话,可以通过@RequestParam的require控制这个参数是否必须提供。
@PostMapping("/net.tongfu.restful.require")
public Map<String, Object> methodRequire(
String queryParam1,
Integer queryParam2,
@RequestParam(required = false) String postParam1
){
Map<String, Object> map = new LinkedHashMap<>();
map.put("queryParam1", queryParam1);
map.put("queryParam2", queryParam2);
map.put("postParam1", postParam1);
return map;
}
9. JSON
如果我们需要处理JSON数据,就需要通过@RequestBody去接收RAW数据,然后再去解析这个RAW数据。
@PostMapping("/net.tongfu.restful.json")
public Map<String, Object> methodQueryAndPost(
String param1,
Integer param2,
@RequestBody String jsonString
){
Map<String, Object> map = new LinkedHashMap<>();
JSONObject jsonObject;
jsonObject = JSONObject.fromObject(jsonString);
map.put("queryParam1", param1);
map.put("queryParam2", param2);
map.put("jsonParam1", jsonObject.get("param1").toString());
map.put("jsonParam2", Integer.valueOf(jsonObject.get("param2").toString()));
return map;
}
10. 总结
今天福哥带着童鞋们针对SpringBoot框架的RESTFul风格接口的各种请求方式的参数接收的方法进行了一个系统的学习,在使用SpringBoot开发RESTFul接口的时候,这些技术是必须熟练掌握的~~
https://tongfu.net/home/35/blog/513581.html
- 上一篇: ES6 fetch()方法详解
- 下一篇: https ssl 请求过程详解
猜你喜欢
- 2024-12-03 Yii::$app->request常用属性和方法
- 2024-12-03 查缺补漏:一文看懂HTTP请求流程,不信你还不会
- 2024-12-03 Mysql写入频繁,怎么破?这是我见过的最清晰的“神操作”
- 2024-12-03 “我将 AWS 的数据传输成本,降低了 99%!”
- 2024-12-03 必备 Python 库:Requests - 轻松完成 HTTP 请求
- 2024-12-03 免费获取韵达快递查询API的使用指南
- 2024-12-03 手撸了一个网络请求工具类,开发速度迅速提升了300%
- 2024-12-03 应用层协议HTTP和HTTPS,一篇文章学会
- 2024-12-03 HTTP客户端连接,选择HttpClient还是OkHttp?
- 2024-12-03 深入理解 HTTP 请求参数和响应参数
- 最近发表
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- md5 sha1 (32)
- mybatis plus page (35)
- semaphore 使用详解 (32)
- update from 语句 (32)
- vue @scroll (38)
- 堆栈区别 (33)
- 在线子域名爆破 (32)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)