文章目录
- 传参
- http 状态码
- 传参方式
- (1)URL请求参数 key 与 方法中的形参名一致
- (2)URL请求参数 key与@RequestParam("id") 中的别名一致
- (3) 形参是POJO类,URL 参数 key 与pojo类的 set方法一致
- (4)参数属于URL一部分,作为路径上的变量
- ReceiveAddress.java
- 通过反射生成对象,并给对象赋值
- 传参
传参
http 状态码
(1)200: 响应成功
(2)404 :资源找不到 - 路径错误(URL错了)
(3)405: 请求方式没有对应好(type=Method Not Allowed, status=405)
(4)400: 请求的参数与服务器类型不匹配
(5)500: 服务器后端代码出错了(出现了Exception)
JavaBean : 实体类 (1)无参构造函数(2)属性有get,set方法
传参方式
(1)URL请求参数 key 与 方法中的形参名一致
// http://localhost:8080/app/fun1?username=smith&userAge=20
@GetMapping("fun1")
@ResponseBody
public String fun1(String username,Integer userAge){
System.out.println(username);
System.out.println(userAge);
return "fun1";
}
(2)URL请求参数 key与@RequestParam(“id”) 中的别名一致
@RequestParam: URL请求参数 key与@RequestParam(“id”) 中的别名一致
// http://localhost:8080/app/fun2?id=101&username=小林&telno=18943049303
@GetMapping("fun2")
@ResponseBody
public String fun2(@RequestParam("id") Integer addrId,
@RequestParam("telno")Long receiveUserTelno,
@RequestParam("username")String receiveUsername){
String result = "addrId:" + addrId + "receiveUsername:" + receiveUsername + "receiveUserTelno :"+receiveUserTelno;
return result;
}
(3) 形参是POJO类,URL 参数 key 与pojo类的 set方法一致
参数是pojo类, 要有(1)构造函数 (2)get,set
URL 参数 key 与pojo类的 set方法一致(set方法名去除set单词后,首字母小写)
底层执行原理
(1)通过反射创建ReceiveAddress对象
(2)通过反射调用set方法赋值
@GetMapping("fun3")
@ResponseBody
public ReceiveAddress fun3(ReceiveAddress address){ // javabean (pojo): 无参构造函数 + get,set
return address;
}
(4)参数属于URL一部分,作为路径上的变量
参数属于URL一部分,作为路径上的变量
//URL : http://localhost:8080/app/receiveAddress/103
@GetMapping("{addrId}")
@ResponseBody // 返回json数据
public ReceiveAddress getById(@PathVariable("addrId") Integer addrId){
return null;
}
ReceiveAddress.java
package com.example.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.time.LocalDateTime;
@TableName("receive_address")
public class ReceiveAddress implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "addr_id", type = IdType.AUTO)
private Integer addrId;
private Long receiveUserTelno;
private String username;
private Integer custId;
/**
* 地址的省份
*/
private String addrProvince;
/**
* 地址的城市
*/
private String addrCity;
/**
* 地址的区域
*/
private String addrArea;
/**
* 地址的街道
*/
private String addrStreet;
/**
* 详细地址
*/
private String addrDetail;
/**
* 状态
*/
private Integer status;
/**
* 版本号,用于做乐观锁
*/
private Integer version;
/**
* 数据添加的时间
*/
private LocalDateTime createTime;
/**
* 数据修改时间
*/
private LocalDateTime updateTime;
public ReceiveAddress(Integer addrId, Long receiveUserTelno, String receiveUsername, Integer custId, String addrProvince, String addrCity, String addrArea, String addrStreet, String addrDetail, Integer status, Integer version, LocalDateTime createTime, LocalDateTime updateTime) {
System.out.println("调用ReceiveAddress 的有参构造函数>>>>>>>>>>>>>>>.");
this.addrId = addrId;
this.receiveUserTelno = receiveUserTelno;
this.username = receiveUsername;
this.custId = custId;
this.addrProvince = addrProvince;
this.addrCity = addrCity;
this.addrArea = addrArea;
this.addrStreet = addrStreet;
this.addrDetail = addrDetail;
this.status = status;
this.version = version;
this.createTime = createTime;
this.updateTime = updateTime;
}
public Integer getAddrId() {
return addrId;
}
public void setAddrId(Integer addrId) {
this.addrId = addrId;
}
public Long getReceiveUserTelno() {
return receiveUserTelno;
}
public void setReceiveUserTelno(Long receiveUserTelno) {
this.receiveUserTelno = receiveUserTelno;
}
public String getReceiveUsername() {
return username;
}
public void setReceiveUsername(String receiveUsername) {
System.out.println(".................调用了setReceiveUsername");
this.username = receiveUsername;
}
public Integer getCustId() {
return custId;
}
public void setCustId(Integer custId) {
this.custId = custId;
}
public String getAddrProvince() {
return addrProvince;
}
public void setAddrProvince(String addrProvince) {
this.addrProvince = addrProvince;
}
public String getAddrCity() {
return addrCity;
}
public void setAddrCity(String addrCity) {
this.addrCity = addrCity;
}
public String getAddrArea() {
return addrArea;
}
public void setAddrArea(String addrArea) {
this.addrArea = addrArea;
}
public String getAddrStreet() {
return addrStreet;
}
public void setAddrStreet(String addrStreet) {
this.addrStreet = addrStreet;
}
public String getAddrDetail() {
return addrDetail;
}
public void setAddrDetail(String addrDetail) {
this.addrDetail = addrDetail;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public LocalDateTime getCreateTime() {
return createTime;
}
public void setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
}
public LocalDateTime getUpdateTime() {
return updateTime;
}
public void setUpdateTime(LocalDateTime updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "ReceiveAddress{" +
"addrId=" + addrId +
", receiveUserTelno=" + receiveUserTelno +
", receiveUsername=" + username +
", custId=" + custId +
", addrProvince=" + addrProvince +
", addrCity=" + addrCity +
", addrArea=" + addrArea +
", addrStreet=" + addrStreet +
", addrDetail=" + addrDetail +
", status=" + status +
", version=" + version +
", createTime=" + createTime +
", updateTime=" + updateTime +
"}";
}
}
通过反射生成对象,并给对象赋值
/*
ReceiveAddress address
1. 创建对象
2. 属性赋值
new : 创建对象
构造函数的作用: 初始化对象 initial
*/
// ReceiveAddress address = new ReceiveAddress();
// address.setReceiveUsername("smith");
// (1)通过反射创建ReceiveAddress 对象 -- 前提:ReceiveAddress 有无参构造函数
ReceiveAddress address = ReceiveAddress.class.newInstance(); // 会调用 无参构造函数
//(2) 通过反射调用set方法 setReceiveUsername(String)
Method method = ReceiveAddress.class.getMethod("setReceiveUsername",String.class);
method.invoke(address,"dd");
传参
package com.example.controller;
import com.example.entity.ReceiveAddress;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import java.util.Locale;
/**
* 请求的相关参数
*/
@Controller
public class HelloController {
// http://localhost:8080/app/fun1?username=smith&userAge=20
@GetMapping("fun1")
@ResponseBody
public String fun1(String username,Integer userAge){
System.out.println(username);
System.out.println(userAge);
return "fun1";
}
@GetMapping("fun2")
@ResponseBody
public String fun2(@RequestParam("id") Integer addrId,
@RequestParam("telno")Long receiveUserTelno,
@RequestParam("username")String receiveUsername){
String result = "addrId:" + addrId + "receiveUsername:" + receiveUsername + "receiveUserTelno :"+receiveUserTelno;
return result;
}
// http://localhost:8080/app/fun3?addrId=101&receiveUsername=%E5%B0%8F%E7%8E%8B&receiveUserTelno=18943049303
// 实参的key 与 pojo的属性名相同
// 面向对象 + 反射
/**
* (1)通过反射创建ReceiveAddress 对象 -- 前提:ReceiveAddress 调用无参构造函数
* (2)通过反射set方法,参数赋值
*/
@GetMapping("fun3")
@ResponseBody
public ReceiveAddress fun3(ReceiveAddress address){ // javabean (pojo): 无参构造函数 + get,set
return address;
}
/*
Customer (custId,custName,custPwd --- getters,setters)
URL : ....?custId=101&custName=smith&custPwd=abc123
*/
@GetMapping("{addrId}") //URL : http://localhost:8080/app/receiveAddress/103
@ResponseBody // 返回json数据
public ReceiveAddress getById(@PathVariable("addrId") Integer addrId){
return null;
}
}