文章目录
- SpringBoot Controller获取请求参数
- Get方法
- 1.参数在路径中
- 2.参数在?后
- 3.map
- 4.数组
- 5.对象
- 5.1单对象
- 5.2多对象
- Post方法
- 1.form表单
- 1.1).map
- 1.2).数组
- 1.3).对象(重点)
- 1.4).多对象
- 2.字符串文本
- 3.json格式-使用对象接收(重点)
SpringBoot Controller获取请求参数
总结了一些常用的get post请求的参数,结合了一些小demo,方便理解。
Get方法
1.参数在路径中
restful风格的url(啥叫restful? 查询用get,新增post,修改put,删除delete)
http://localhost:8080/hello/qwl
代码
package com.qwl.getpost.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GetController {
/**
*restful查询请求
* @param name
* @return
*/
@GetMapping("/hello/{name}")
public String getHello(@PathVariable("name") String name){
return "hello!"+name;
}
}
结果
2.参数在?后
url
http://localhost:8080/hello?name=qwl
代码
package com.qwl.getpost.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GetController {
/**
*写在问号后的请求
* @param name
* @return
*/
@GetMapping("/hello")
public String getHello1(@RequestParam("name") String name){
return "hello----"+name;
}
}
结果
无参传递会报错,记得加 required = false
url
http://localhost:8080/hello
代码
@RestController
public class GetController {
/**
*无参不报错
* @param name
* @return
*/
@GetMapping("/hello")
public String getHello1(@RequestParam(name="name",required = false) String name){
return "hello----"+name;
}
}
结果
无参传递可以赋默认值,加上 defaultValue = "阿猫阿狗"
url
http://localhost:8080/hello
代码
package com.qwl.getpost.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GetController {
/**
*无参赋默认值
* @param name
* @return
*/
@GetMapping("/hello")
public String getHello1(@RequestParam(name="name",defaultValue = "阿猫阿狗") String name){
return "hello----"+name;
}
}
结果
3.map
url
http://localhost:8080/helloMap?name=qwl&age=24
代码
package com.qwl.getpost.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class GetController {
/**
* map接收参数
* @param map
* @return
*/
@GetMapping("/helloMap")
public String getHelloMap(@RequestParam Map<String,String> map){
return "大家好,我叫:"+map.get("name")+"!今年"+map.get("age")+"岁了~";
}
}
结果
4.数组
url
http://localhost:8080//helloArr?name=qwl&name=uzi&name=theShy
代码
package com.qwl.getpost.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class GetController {
/**
* 数组接收参数
* @param nameArr
* @return
*/
@GetMapping("/helloArr")
public String getHelloArr(@RequestParam("name") String[] nameArr){
String str = "";
for (String s : nameArr) {
str += s + "、";
}
return "表扬以下同学:"+str;
}
}
结果
5.对象
5.1单对象
url
http://localhost:8080//helloEntity?name=qwl&age=24
代码
-新建学生类
package com.qwl.getpost.entity;
public class Student {
/**
* 姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
-controller
package com.qwl.getpost.controller;
import com.qwl.getpost.entity.Student;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class GetController {
/**
* 单对象接收参数
* @param student
* @return
*/
@GetMapping("/helloEntity")
public String getHelloEntity(Student student){
return "大家好,我叫"+student.getName()+",今年"+student.getAge()+"岁";
}
}
结果
5.2多对象
url
http://localhost:8080//helloEntites?name=qwl&age=24&phoneName=华为&phoneNumber=15333333333
代码
-新建手机类
package com.qwl.getpost.entity;
public class Phone {
/**
* 手机名称
*/
private String phoneName;
/**
* 手机号码
*/
private String phoneNumber;
public String getPhoneName() {
return phoneName;
}
public void setPhoneName(String phoneName) {
this.phoneName = phoneName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
-controller
package com.qwl.getpost.controller;
import com.qwl.getpost.entity.Phone;
import com.qwl.getpost.entity.Student;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class GetController {
@GetMapping("/helloEntites")
public String getHelloEntites(Student student, Phone phone){
return "大家好,我叫"+student.getName()+",今年"+student.getAge()+"岁!" +
"我用的这款手机是:"+phone.getPhoneName()+",我的手机号是:"+phone.getPhoneNumber();
}
}
结果
Post方法
这里统一使用postman进行测试
1.form表单
1.1).map
url
localhost:8080//postByHelloMap
代码
package com.qwl.getpost.controller;
import com.qwl.getpost.entity.Phone;
import com.qwl.getpost.entity.Student;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
public class GetController {
/**
* post,formData用map接收参数
* @param map
* @return
*/
@PostMapping("/postByHelloMap")
public String postByHelloMap(@RequestParam Map<String,String> map){
return "大家好,我叫:"+map.get("name")+",今年:"+map.get("age")+"岁了";
}
}
结果
1.2).数组
url
localhost:8080//postByHelloArr
代码
package com.qwl.getpost.controller;
import com.qwl.getpost.entity.Phone;
import com.qwl.getpost.entity.Student;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
public class GetController {
/**
* post,formData用数组接收参数
* @param nameArr
* @return
*/
@PostMapping("/postByHelloArr")
public String postByHelloArr(@RequestParam("name") String[] nameArr){
String str = "";
for (String name : nameArr) {
str += name + ",";
}
return str+"祝大家新年快乐";
}
}
结果
1.3).对象(重点)
url
localhost:8080//postByEntity
代码
package com.qwl.getpost.controller;
import com.qwl.getpost.entity.Phone;
import com.qwl.getpost.entity.Student;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
public class GetController {
/**
* post,formData用对象接收参数
* @param student
* @return
*/
@PostMapping("/postByEntity")
public String postByEntity(Student student){
return "大家好,我叫:"+student.getName()+",今年:"+student.getAge()+"岁了";
}
}
结果
1.4).多对象
url
localhost:8080//postByEntities
代码
package com.qwl.getpost.controller;
import com.qwl.getpost.entity.Phone;
import com.qwl.getpost.entity.Student;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
public class GetController {
/**
* post,formData用多对象接收参数
* @param student
* @return
*/
@PostMapping("/postByEntities")
public String postByEntities(Student student,Phone phone){
return "大家好,我叫:"+student.getName()+",今年:"+student.getAge()+"岁了,我的手机号是"+phone.getPhoneNumber();
}
}
结果
2.字符串文本
text文本可通过HttpServletRequest获取输入流再接收文本内容
url
localhost:8080//postByText
代码
package com.qwl.getpost.controller;
import com.qwl.getpost.entity.Phone;
import com.qwl.getpost.entity.Student;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
@RestController
public class GetController {
/**
* 文本接收
*
* @param request
* @return
*/
@PostMapping("/postByText")
public String postByText(HttpServletRequest request) {
ServletInputStream is = null;
try {
is = request.getInputStream();
StringBuilder sb = new StringBuilder();
byte[] buf = new byte[1024];
int len = 0;
while ((len = is.read(buf)) != -1) {
sb.append(new String(buf, 0, len));
}
return "文本:" + sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
结果
3.json格式-使用对象接收(重点)
url
localhost:8080//postByJsonEntity
代码
package com.qwl.getpost.controller;
import com.qwl.getpost.entity.Phone;
import com.qwl.getpost.entity.Student;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
public class GetController {
/**
* post,json格式,对象接收参数
* @param student
* @return
*/
@PostMapping("/postByJsonEntity")
public String postByJsonEntity(@RequestBody Student student){
return "大家好,我叫:"+student.getName()+",今年:"+student.getAge()+"岁了";
}
}
结果