博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Springboot 请求数据
阅读量:2134 次
发布时间:2019-04-30

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

请求方式

get
  1. 用RequestMapping
@RequestMapping("/hello") // 默认为get方式,或者可以写成@RequestMapping(value="/hello", method.RequestMethod.GET)public String say() {
return "hello";}
  1. 用GetMapping
@GetMapping("/hello1")public String say1() {
return "hello";}
  1. 有参方法
    请求时在浏览器中输入: localhost:8080/hello?name=zhangsan&age=18
    这个实体类里应有get set方法,要不返回为空
@RequestMapping("/hello2")public String say2(Person person) {
return person.toString();}public class Person {
private String name; private int age; public String getName() {
return name; } public int getAge() {
return age; } public void setName(String name) {
this.name = name; } public void setAge(int age) {
this.age = age; } public String toString() {
return "name:" + name + ";age:" + age; }}

请求时在浏览器中输入: localhost:8080/hello3/77?name=zhangsan&age=18

@RequestMapping("/hello3/{id}")public String say3(@PathVariable(value="id") Integer id,				   @RequestParam(value="name") String name,				   @RequestParam(value="age", required=false, defaultValue="1000") String age				){
return "id: " + id + ", name: " + name + ", age: " + age;}
post
  1. 用RequestMapping
@RequestMapping("/hello4", method=RequestMethod.POST)public String say4() {
return "hello 4";}
  1. 用PostMapping
@PostMapping("/hello5")public String say5() {
return "hello 5";}
  1. 有参方法,模拟post请求需要下载postman等插件进行模拟
    与get区别在于应在实体类上加@RequestBody注解,原因未知
@PostMapping("/hello6")public String say6(@RequestBody Person person) {
System.out.println(person.toString()); return person.toString();}

post发送请求用x-www-form-urlencoded格式可以传递参数,用form-data只会有默认值,具体区别请参考

@PostMapping("/hello7")public String say7(@RequestParam(value="name") String name,				   @RequestParam(value="age") String age) {
String result = "name: " + name + ", age:" + age; System.out.println(result); return result;}

http请求格式

请求部分 请求内容
状态行(请求方法+请求协议) GET /test.html HTTP/1.1
请求头 Accept: image/gif
请求体
POST提交数据方式 区别
application/x-www-form-urlencoded
multipart/form-data
application/json
text/xml

转载地址:http://oqugf.baihongyu.com/

你可能感兴趣的文章
Leetcode Go 《精选TOP面试题》20200628 69.x的平方根
查看>>
Leetcode C++ 剑指 Offer 09. 用两个栈实现队列
查看>>
Leetcode C++《每日一题》20200707 112. 路径总和
查看>>
云原生 第十一章 应用健康
查看>>
Leetcode C++ 《第202场周赛》
查看>>
云原生 第十二章 可观测性:监控与日志
查看>>
Leetcode C++ 《第203场周赛》
查看>>
云原生 第十三章 Kubernetes网络概念及策略控制
查看>>
《redis设计与实现》 第一部分:数据结构与对象 || 读书笔记
查看>>
《redis设计与实现》 第二部分(第9-11章):单机数据库的实现
查看>>
算法工程师 面经2019年5月
查看>>
搜索架构师 一面面经2019年6月
查看>>
稻草人手记
查看>>
第一次kaggle比赛 回顾篇
查看>>
leetcode 50. Pow(x, n)
查看>>
leetcode 130. Surrounded Regions
查看>>
【托业】【全真题库】TEST2-语法题
查看>>
博客文格式优化
查看>>
【托业】【新托业全真模拟】疑难语法题知识点总结(01~05)
查看>>
【SQL】group by 和order by 的区别。
查看>>