注意点
- 无注解传参
- 方式一:参数名字与服务端参数一致,自动绑定
- method(String param1, String param2)
- 方式二:传封装实体参数
- method(RequestDTO requestBody)
- 方式一:参数名字与服务端参数一致,自动绑定
- 数组参数:
- 方式一:使用@RequestParam解析相同参数,然后将相同参数的值组成一个数组
- method(@RequestParam List<String> param3)
- 方式二:使用@RequestBody 直接传数组(推荐)
- method(@RequestBody RequestDTO requestBody)
- 方式一:使用@RequestParam解析相同参数,然后将相同参数的值组成一个数组
- @PathVariable只能解析单个参数,无法解析数组,可以借助@RequestParam或@RequestBody
Axios封装
import axios from 'axios';
// 创建 axios 实例
const instance = axios.create({
baseURL: 'http://localhost:8080/api/example', // 基础 URL
timeout: 5000, // 请求超时时间
});
// 封装通用的请求方法
const request = async (method, url, data = {}, params = {}) => {
try {
const response = await instance({
method,
url,
data, // POST/PUT 请求体
params, // GET/DELETE 请求参数
});
return response.data;
} catch (error) {
console.error('Request failed:', error);
throw error;
}
};
export default request;
GET 请求
无参请求
服务端
@GetMapping("/getNoAnnotation")
public String getNoAnnotation(String param1, String param2, @RequestParam List<String> param3) {
return "GET No Annotation: param1=" + param1 + ", param2=" + param2 + ", param3=" + param3;
}
客户端

import request from './request';
const getNoAnnotation = async (param1, param2, param3) => {
return request('get', '/getNoAnnotation', {}, {
param1,
param2,
param3, // 重复添加 param3 参数
});
};
// 调用示例
getNoAnnotation('value1', 'value2', ['item1', 'item2'])
.then(response => console.log(response))
.catch(error => console.error(error));
@RequestParam
服务端
@GetMapping("/getWithAnnotation")
public String getWithAnnotation(@RequestParam String param1, @RequestParam String param2, @RequestParam List<String> param3) {
return "GET With Annotation: param1=" + param1 + ", param2=" + param2 + ", param3=" + param3;
}
客户端

import request from './request';
const getWithAnnotation = async (param1, param2, param3) => {
return request('get', '/getWithAnnotation', {}, {
param1,
param2,
param3, // 重复添加 param3 参数
});
};
// 调用示例
getWithAnnotation('value1', 'value2', ['item1', 'item2'])
.then(response => console.log(response))
.catch(error => console.error(error));
@PathVariable
服务端
@GetMapping("/getWithPathVariable/{param1}/{param2}")
public String getWithPathVariable(@PathVariable String param1, @PathVariable String param2, @RequestParam List<String> param3) {
return "GET With Path Variable: param1=" + param1 + ", param2=" + param2 + ", param3=" + param3;
}
客户端

import request from './request';
const getWithPathVariable = async (param1, param2, param3) => {
return request('get', `/getWithPathVariable/${param1}/${param2}`, {}, {
param3, // 重复添加 param3 参数
});
};
// 调用示例
getWithPathVariable('value1', 'value2', ['item1', 'item2'])
.then(response => console.log(response))
.catch(error => console.error(error));
Post请求
无参请求
服务端
@PostMapping("/postNoAnnotation")
public String postNoAnnotation(String param1, String param2, @RequestParam List<String> param3) {
return "POST No Annotation: param1=" + param1 + ", param2=" + param2 + ", param3=" + param3;
}
客户端

import request from './request';
const postNoAnnotation = async (param1, param2, param3) => {
return request('post', '/postNoAnnotation', {}, {
param1,
param2,
param3, // 重复添加 param3 参数
});
};
// 调用示例
postNoAnnotation('value1', 'value2', ['item1', 'item2'])
.then(response => console.log(response))
.catch(error => console.error(error));
@RequestParam
服务端
@PostMapping("/postWithAnnotation")
public String postWithAnnotation(@RequestParam String param1, @RequestParam String param2, @RequestParam List<String> param3) {
return "POST With Annotation: param1=" + param1 + ", param2=" + param2 + ", param3=" + param3;
}
客户端

import request from './request';
const postWithAnnotation = async (param1, param2, param3) => {
return request('post', '/postWithAnnotation', {}, {
param1,
param2,
param3, // 重复添加 param3 参数
});
};
// 调用示例
postWithAnnotation('value1', 'value2', ['item1', 'item2'])
.then(response => console.log(response))
.catch(error => console.error(error));
@PathVariable
服务端
@PostMapping("/postWithPathVariable/{param1}/{param2}")
public String postWithPathVariable(@PathVariable String param1, @PathVariable String param2, @RequestParam List<String> param3) {
return "POST With Path Variable: param1=" + param1 + ", param2=" + param2 + ", param3=" + param3;
}
客户端

import request from './request';
const postWithPathVariable = async (param1, param2, param3) => {
return request('post', `/postWithPathVariable/${param1}/${param2}`, {}, {
param3, // 重复添加 param3 参数
});
};
// 调用示例
postWithPathVariable('value1', 'value2', ['item1', 'item2'])
.then(response => console.log(response))
.catch(error => console.error(error));
请求body无注解
服务端
@PostMapping("/postNoWithBody")
public String postNoWithBody( RequestDTO requestBody) {
return "POST No With Body: " + requestBody.toString();
}
客户端

import request from './request';
const postNoWithBody = async (requestBody) => {
return request('post', '/postNoWithBody', requestBody);
};
// 调用示例
postNoWithBody({ param1: 'value1', param2: 'value2',param3:['item1','item2'] })
.then(response => console.log(response))
.catch(error => console.error(error));
@RequestBody
服务端
@PostMapping("/postWithBody")
public String postWithBody(@RequestBody RequestDTO requestBody) {
return "POST With Body: " + requestBody.toString();
}
客户端
http://localhost:8080/api/example/postWithBody

import request from './request';
const postWithBody = async (requestBody) => {
return request('post', '/postWithBody', requestBody);
};
// 调用示例
postWithBody({ param1: 'value1', param2: 'value2',param3:['item1','item2'] })
.then(response => console.log(response))
.catch(error => console.error(error));
Put/Delete请求与Post相似
完整用例
Java
import org.example.domain.dto.RequestDTO;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/example")
public class RequestExampleController {
// GET
@GetMapping("/getNoAnnotation")
public String getNoAnnotation(String param1, String param2, @RequestParam List<String> param3) {
return "GET No Annotation: param1=" + param1 + ", param2=" + param2 + ", param3=" + param3;
}
@GetMapping("/getWithAnnotation")
public String getWithAnnotation(@RequestParam String param1, @RequestParam String param2, @RequestParam List<String> param3) {
return "GET With Annotation: param1=" + param1 + ", param2=" + param2 + ", param3=" + param3;
}
@GetMapping("/getWithPathVariable/{param1}/{param2}")
public String getWithPathVariable(@PathVariable String param1, @PathVariable String param2, @RequestParam List<String> param3) {
return "GET With Path Variable: param1=" + param1 + ", param2=" + param2 + ", param3=" + param3;
}
// POST 无参请求不能绑定数组,只能绑定单个参数,可以使用 @RequestParam或 @RequestBody
@PostMapping("/postNoAnnotation")
public String postNoAnnotation(String param1, String param2, @RequestParam List<String> param3) {
return "POST No Annotation: param1=" + param1 + ", param2=" + param2 + ", param3=" + param3;
}
@PostMapping("/postWithAnnotation")
public String postWithAnnotation(@RequestParam String param1, @RequestParam String param2, @RequestParam List<String> param3) {
return "POST With Annotation: param1=" + param1 + ", param2=" + param2 + ", param3=" + param3;
}
@PostMapping("/postWithPathVariable/{param1}/{param2}")
public String postWithPathVariable(@PathVariable String param1, @PathVariable String param2, @RequestParam List<String> param3) {
return "POST With Path Variable: param1=" + param1 + ", param2=" + param2 + ", param3=" + param3;
}
@PostMapping("/postNoWithBody")
public String postNoWithBody( RequestDTO requestBody) {
return "POST No With Body: " + requestBody.toString();
}
@PostMapping("/postWithBody")
public String postWithBody(@RequestBody RequestDTO requestBody) {
return "POST With Body: " + requestBody.toString();
}
// PUT
@PutMapping("/putNoAnnotation")
public String putNoAnnotation(String param1, String param2, @RequestParam List<String> param3) {
return "PUT No Annotation: param1=" + param1 + ", param2=" + param2 + ", param3=" + param3;
}
@PutMapping("/putWithAnnotation")
public String putWithAnnotation(@RequestParam String param1, @RequestParam String param2, @RequestParam List<String> param3) {
return "PUT With Annotation: param1=" + param1 + ", param2=" + param2 + ", param3=" + param3;
}
@PutMapping("/putWithPathVariable/{param1}/{param2}")
public String putWithPathVariable(@PathVariable String param1, @PathVariable String param2, @RequestParam List<String> param3) {
return "PUT With Path Variable: param1=" + param1 + ", param2=" + param2 + ", param3=" + param3;
}
@PutMapping("/putNoWithBody")
public String putNoWithBody( RequestDTO requestBody) {
return "PUT No With Body: " + requestBody.toString();
}
@PutMapping("/putWithBody")
public String putWithBody(@RequestBody RequestDTO requestBody) {
return "PUT With Body: " + requestBody.toString();
}
// DELETE
@DeleteMapping("/deleteNoAnnotation")
public String deleteNoAnnotation(String param1, String param2, @RequestParam List<String> param3) {
return "DELETE No Annotation: param1=" + param1 + ", param2=" + param2 + ", param3=" + param3;
}
@DeleteMapping("/deleteWithAnnotation")
public String deleteWithAnnotation(@RequestParam String param1, @RequestParam String param2, @RequestParam List<String> param3) {
return "DELETE With Annotation: param1=" + param1 + ", param2=" + param2 + ", param3=" + param3;
}
@DeleteMapping("/deleteWithPathVariable/{param1}/{param2}")
public String deleteWithPathVariable(@PathVariable String param1, @PathVariable String param2, @RequestParam List<String> param3) {
return "DELETE With Path Variable: param1=" + param1 + ", param2=" + param2 + ", param3=" + param3;
}
}
import java.util.List;
public class RequestDTO {
private String param1;
private String param2;
List<String> param3;
// Getters and Setters
public String getParam1() {
return param1;
}
public void setParam1(String param1) {
this.param1 = param1;
}
public String getParam2() {
return param2;
}
public void setParam2(String param2) {
this.param2 = param2;
}
public List<String> getParam3() {
return param3;
}
public void setParam3(List<String> param3) {
this.param3 = param3;
}
@Override
public String toString() {
return "RequestBodyExample{" +
"param1='" + param1 + '\'' +
", param2='" + param2 + '\'' +
", param3=" + param3 + '\'' +
'}';
}
}
前端
import request from './request'; // 引入封装的 request 方法
// GET 请求
export const getNoAnnotation = (param1, param2, param3) => {
return request('get', '/getNoAnnotation', {}, {
param1,
param2,
param3, // 重复添加 param3 参数
});
};
export const getWithAnnotation = (param1, param2, param3) => {
return request('get', '/getWithAnnotation', {}, {
param1,
param2,
param3, // 重复添加 param3 参数
});
};
export const getWithPathVariable = (param1, param2, param3) => {
return request('get', `/getWithPathVariable/${param1}/${param2}`, {}, {
param3, // 重复添加 param3 参数
});
};
// POST 请求
export const postNoAnnotation = (param1, param2, param3) => {
return request('post', '/postNoAnnotation', {}, {
param1,
param2,
param3, // 重复添加 param3 参数
});
};
export const postWithAnnotation = (param1, param2, param3) => {
return request('post', '/postWithAnnotation', {}, {
param1,
param2,
param3, // 重复添加 param3 参数
});
};
export const postWithPathVariable = (param1, param2, param3) => {
return request('post', `/postWithPathVariable/${param1}/${param2}`, {}, {
param3, // 重复添加 param3 参数
});
};
export const postNoWithBody = (requestBody) => {
return request('post', '/postNoWithBody', requestBody);
};
export const postWithBody = (requestBody) => {
return request('post', '/postWithBody', requestBody);
};
// PUT 请求
export const putNoAnnotation = (param1, param2, param3) => {
return request('put', '/putNoAnnotation', {}, {
param1,
param2,
param3, // 重复添加 param3 参数
});
};
export const putWithAnnotation = (param1, param2, param3) => {
return request('put', '/putWithAnnotation', {}, {
param1,
param2,
param3, // 重复添加 param3 参数
});
};
export const putWithPathVariable = (param1, param2, param3) => {
return request('put', `/putWithPathVariable/${param1}/${param2}`, {}, {
param3, // 重复添加 param3 参数
});
};
export const putNoWithBody = (requestBody) => {
return request('put', '/putNoWithBody', requestBody);
};
export const putWithBody = (requestBody) => {
return request('put', '/putWithBody', requestBody);
};
// DELETE 请求
export const deleteNoAnnotation = (param1, param2, param3) => {
return request('delete', '/deleteNoAnnotation', {}, {
param1,
param2,
param3, // 重复添加 param3 参数
});
};
export const deleteWithAnnotation = (param1, param2, param3) => {
return request('delete', '/deleteWithAnnotation', {}, {
param1,
param2,
param3, // 重复添加 param3 参数
});
};
export const deleteWithPathVariable = (param1, param2, param3) => {
return request('delete', `/deleteWithPathVariable/${param1}/${param2}`, {}, {
param3, // 重复添加 param3 参数
});
};
import {
getNoAnnotation,
getWithAnnotation,
getWithPathVariable,
postNoAnnotation,
postWithAnnotation,
postWithPathVariable,
postNoWithBody,
postWithBody,
putNoAnnotation,
putWithAnnotation,
putWithPathVariable,
putNoWithBody,
putWithBody,
deleteNoAnnotation,
deleteWithAnnotation,
deleteWithPathVariable,
} from './api';
// 调用示例
getNoAnnotation('value1', 'value2', ['item1', 'item2'])
.then(response => console.log(response))
.catch(error => console.error(error));
postWithBody({ { param1: 'value1', param2: 'value2',param3:['item1','item2'] } })
.then(response => console.log(response))
.catch(error => console.error(error));
deleteWithPathVariable('value1', 'value2', ['item1', 'item2'])
.then(response => console.log(response))
.catch(error => console.error(error));
postman
{
"info": {
"_postman_id": "68a4b07f-9070-4e5c-bff6-7a6f53b9ae85",
"name": "常见请求方式",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "43125700"
},
"item": [
{
"name": "getWithAnnotation",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:8080/api/example/getWithAnnotation?param1=value1¶m2=value2¶m3=item1¶m3=item2",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"example",
"getWithAnnotation"
],
"query": [
{
"key": "param1",
"value": "value1"
},
{
"key": "param2",
"value": "value2"
},
{
"key": "param3",
"value": "item1"
},
{
"key": "param3",
"value": "item2"
}
]
}
},
"response": []
},
{
"name": "getWithAnnotation",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:8080/api/example/getWithAnnotation?param1=value1¶m2=value2¶m3=item1¶m3=item2",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"example",
"getWithAnnotation"
],
"query": [
{
"key": "param1",
"value": "value1"
},
{
"key": "param2",
"value": "value2"
},
{
"key": "param3",
"value": "item1"
},
{
"key": "param3",
"value": "item2"
}
]
}
},
"response": []
},
{
"name": "getWithPathVariable",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:8080/api/example/getWithPathVariable/value1/value2?param3=item1¶m3=item2",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"example",
"getWithPathVariable",
"value1",
"value2"
],
"query": [
{
"key": "param3",
"value": "item1"
},
{
"key": "param3",
"value": "item2"
}
]
}
},
"response": []
},
{
"name": "postNoAnnotation",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "param1",
"value": "value1",
"type": "text"
},
{
"key": "param2",
"value": "value2",
"type": "text"
},
{
"key": "param3",
"value": "item1",
"type": "text"
},
{
"key": "param3",
"value": "item2",
"type": "text"
}
]
},
"url": {
"raw": "http://localhost:8080/api/example/postNoAnnotation",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"example",
"postNoAnnotation"
]
}
},
"response": []
},
{
"name": "postWithAnnotation",
"request": {
"method": "POST",
"header": [],
"url": {
"raw": "http://localhost:8080/api/example/postWithAnnotation?param1=value1¶m2=value2¶m3=item1¶m3=item2",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"example",
"postWithAnnotation"
],
"query": [
{
"key": "param1",
"value": "value1"
},
{
"key": "param2",
"value": "value2"
},
{
"key": "param3",
"value": "item1"
},
{
"key": "param3",
"value": "item2"
}
]
}
},
"response": []
},
{
"name": "postWithPathVariable",
"request": {
"method": "POST",
"header": [],
"url": {
"raw": "http://localhost:8080/api/example/postWithPathVariable/value1/value2?param3=item1¶m3=item2",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"example",
"postWithPathVariable",
"value1",
"value2"
],
"query": [
{
"key": "param3",
"value": "item1"
},
{
"key": "param3",
"value": "item2"
}
]
}
},
"response": []
},
{
"name": "postNoWithBody",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "param1",
"value": "value1",
"type": "text"
},
{
"key": "param1",
"value": "value2",
"type": "text"
},
{
"key": "param3",
"value": "item1",
"type": "text"
},
{
"key": "param3",
"value": "item2",
"type": "text"
}
]
},
"url": {
"raw": "http://localhost:8080/api/example/postNoWithBody",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"example",
"postNoWithBody"
]
}
},
"response": []
},
{
"name": "postWithBody",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"param1\": \"value1\",\r\n \"param2\": \"value2\",\r\n \"param3\": [\"item1\",\"item2\"]\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:8080/api/example/postWithBody",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"example",
"postWithBody"
]
}
},
"response": []
},
{
"name": "putNoAnnotation",
"request": {
"method": "PUT",
"header": [],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "param1",
"value": "value1",
"type": "text"
},
{
"key": "param2",
"value": "value2",
"type": "text"
},
{
"key": "param3",
"value": "item1",
"type": "text"
},
{
"key": "param3",
"value": "item2",
"type": "text"
}
]
},
"url": {
"raw": "http://localhost:8080/api/example/putNoAnnotation",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"example",
"putNoAnnotation"
]
}
},
"response": []
},
{
"name": "putWithAnnotation",
"request": {
"method": "PUT",
"header": [],
"url": {
"raw": "http://localhost:8080/api/example/putWithAnnotation?param1=value1¶m2=value2¶m3=item1¶m3=item2",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"example",
"putWithAnnotation"
],
"query": [
{
"key": "param1",
"value": "value1"
},
{
"key": "param2",
"value": "value2"
},
{
"key": "param3",
"value": "item1"
},
{
"key": "param3",
"value": "item2"
}
]
}
},
"response": []
},
{
"name": "putWithPathVariable",
"request": {
"method": "PUT",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"param1\": \"value1\",\r\n \"param2\": \"value2\",\r\n \"param3\": [\"item1\",\"item2\"]\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:8080/api/example/putWithPathVariable",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"example",
"putWithPathVariable"
]
}
},
"response": []
},
{
"name": "putNoWithBody",
"request": {
"method": "PUT",
"header": [],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "param1",
"value": "value1",
"type": "text"
},
{
"key": "param2",
"value": "value2",
"type": "text"
},
{
"key": "param3",
"value": "item1",
"type": "text"
},
{
"key": "param3",
"value": "item2",
"type": "text"
}
]
},
"url": {
"raw": "http://localhost:8080/api/example/putNoWithBody",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"example",
"putNoWithBody"
]
}
},
"response": []
},
{
"name": "putWithBody",
"request": {
"method": "PUT",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"param1\": \"value1\",\r\n \"param2\": \"value2\",\r\n \"param3\": [\"item1\",\"item2\"]\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:8080/api/example/putWithBody",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"example",
"putWithBody"
]
}
},
"response": []
},
{
"name": "deleteNoAnnotation",
"request": {
"method": "DELETE",
"header": [],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "param1",
"value": "value1",
"type": "text"
},
{
"key": "param2",
"value": "value2",
"type": "text"
},
{
"key": "param3",
"value": "item1",
"type": "text"
},
{
"key": "param3",
"value": "item2",
"type": "text"
}
]
},
"url": {
"raw": "http://localhost:8080/api/example/deleteNoAnnotation",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"example",
"deleteNoAnnotation"
]
}
},
"response": []
},
{
"name": "deleteWithAnnotation",
"request": {
"method": "DELETE",
"header": [],
"url": {
"raw": "http://localhost:8080/api/example/deleteWithAnnotation?param1=value1¶m2=value2¶m3=item1¶m3=item2",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"example",
"deleteWithAnnotation"
],
"query": [
{
"key": "param1",
"value": "value1"
},
{
"key": "param2",
"value": "value2"
},
{
"key": "param3",
"value": "item1"
},
{
"key": "param3",
"value": "item2"
}
]
}
},
"response": []
},
{
"name": "deleteWithPathVariable",
"request": {
"method": "DELETE",
"header": [],
"body": {
"mode": "raw",
"raw": "",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:8080/api/example/deleteWithPathVariable/value1/value2?param3=item1¶m3=item2",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"api",
"example",
"deleteWithPathVariable",
"value1",
"value2"
],
"query": [
{
"key": "param3",
"value": "item1"
},
{
"key": "param3",
"value": "item2"
}
]
}
},
"response": []
}
]
}

1503

被折叠的 条评论
为什么被折叠?



