中企动力科技股份有限公司网站官网,做网站有哪些注意事项,东莞本地招聘网站有哪些,wordpress注册默认密码忘记1. RequestParam
RequestParam#xff1a;get请求时如果用不到它的3个属性#xff0c;可以省略#xff1b;其他请求如果是通过param传送数据#xff0c;必须使用该注释 要点#xff1a; 可用于任何类型的请求#xff08;get请求数据在请求行中#xff0c; post请求数据…1. RequestParam
RequestParamget请求时如果用不到它的3个属性可以省略其他请求如果是通过param传送数据必须使用该注释 要点 可用于任何类型的请求get请求数据在请求行中 post请求数据在请求体中无论时在请求行还是请求体格式都是usernamezhangsanpassword1234emailzhangsanpowernode.com 属性
value 不解释看代码defaultValue 设置默认值
// 发送请求
request.get(/user{params:{ // 必须使用paramspeopleAge: 20, }
})GetMapping(/user)
// value属性 指明发送时url中的名字和这里的名字做对应peopleAge对应 age
public String getUser(RequestParam(value peopleAge, defaultValue 18) int age) {return Your age is age;
}required 是否必须传
GetMapping(/user)
public String getUser(RequestParam(required false) String email) {if (email ! null) {return Your email is email;} else {return No email provided.;}
}2. RequestBody
RequestBody 最主要的用处post请求时前端发送json格式字符串后端用类接收 用法 可用于非get请求的其他请求前端发送的是json字符串后端用类接收RequestBody不可以省略required属性是否必需传 PostMapping(/user/{id})
public String updateUser(RequestBody User user) {// 根据id更新用户信息return User updated successfully.;
}请求
import axios from axios;// 假设后端接口地址
const baseUrl http://localhost:8080;// 要发送的数据
const userData {// 假设User类有name和age属性根据实际情况修改name: John Doe,age: 30
};// 发送POST请求的函数
const sendPostRequest async () {try {// 虽然没有在请求头中指明发送的是json格式只要使用post请求就会自动对第二个参数进行json格式化const response await axios.post(/user, userData);console.log(请求成功, response.data);} catch (error) {console.error(请求失败, error);}
};// 调用函数发送请求
sendPostRequest();3. PathVariable
PathVariable发送Rest风格请求时使用 作用 Rest风格请求的占位符可用于任何请求中get、post、put等等也有required 属性 1. 基本使用
/users/123 //请求的urlGetMapping(/users/{userId})
public String getUserById(PathVariable(userId) Long id) {// 根据id从数据库或其他数据源获取用户信息return User with id id retrieved successfully.;
}/orders/100/items/200 // 亲求的url多个GetMapping(/orders/{orderId}/items/{itemId})
public String getOrderItem(PathVariable(orderId) Long orderId, PathVariable(itemId) Long itemId) {// 根据orderId和itemId获取订单中的商品信息return Retrieving item itemId from order orderId;
}2. 与正则表达式配合使用
GetMapping(/users/{userId:\\d})
public String getUserById(PathVariable(userId) String userId) {// 这里可以确保userId是数字格式return User with id userId retrieved successfully.;
}4. 遇到的其他情况 特别注意顺序发送时参数的顺序和接收时参数的顺序 1. get请求发送数据后端用类接收
get请求发送了4个参数 request.get(/admin/list,{params:{currentPage:1,pageSize:10,username:zhangsan,name:张三,}})controller接收
public class AdminController {GetMapping(/list)public ResultPageResult getAdminList(// 参数按顺序接收这里设置了默认值RequestParam(defaultValue 1) Integer currentPage,RequestParam(defaultValue 10) Integer pageSize,// 第3个和第4个参数赋值给了admin类对象Admin admin) {PageInfoAdmin adminList adminService.getAdminList(currentPage, pageSize);return PageResult.ok(adminList);}
}2. Post请求中RequestParam和RequestBody 一起使用
import axios from axios;
const baseUrl http://localhost:8080;
const sendPostRequest async () {try {const response await axios.post(/user, {// 请求体参数userInfo: {name: John Doe,age: 30}}, {// 查询参数params: {id: 123}});console.log(请求成功, response.data);} catch (error) {console.error(请求失败, error);}
};sendPostRequest();PostMapping(/user)
public String saveUser(RequestParam(id) Long id,RequestBody UserInfo userInfo) {// 保存用户逻辑return User saved successfully.;
}