关于Controller请求的相关问题

Controller方法请求注意(二)

1. 方法不添加路径

Controller中的方法上如果不加路径,那么就说明访问Controller文件的路径就访问这个方法,不同的请求方法可以区分不同的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@RestController
@RequestMapping("/server")
public class MeetServerController {

private final MeetServerService meetServerService;

public MeetServerController(MeetServerService meetServerService) {
this.meetServerService = meetServerService;
}

@PostMapping("/list")
public AjaxResult getServers(@RequestBody MeetServerReqVo meetServerReqVo) {
return AjaxResult.success(meetServerService.getServers(meetServerReqVo));
}

@PostMapping
public AjaxResult add(@RequestBody MeetServerReqVo meetServerReqVo) {
meetServerService.add(meetServerReqVo);
return AjaxResult.success();
}

@DeleteMapping
public AjaxResult del(@RequestParam Long id) {
meetServerService.del(id);
return AjaxResult.success();
}

@GetMapping
public AjaxResult getById(@RequestParam Long id) {
return AjaxResult.success(meetServerService.getById(id));
}

@GetMapping("/all")
public AjaxResult getAll() {
return AjaxResult.success(meetServerService.getAll());
}
}

2. 不要求前端必须传参

@RequestParam(required = false)

3. RequestBody和RequestParam

@RequestBody 用于接收请求体中的参数,主要用于接收非表单类型的参数(如 JSON、XML 等)。

1
2
3
4
@PostMapping("/users")
public void createUser(@RequestBody User user) {
// 处理接收到的用户对象
}

@RequestParam 用于接收请求中的查询参数或表单参数,主要用于接收键值对类型的参数。

1
2
3
4
@GetMapping("/users")
public String getUser(@RequestParam("id") int userId) {
// 根据用户ID查询用户
}

4. GET方法不能有请求体

在标准的HTTP规范中,GET 请求是不应该有请求体的。GET 请求的参数通常是通过URL的查询字符串(query string)来传递的,可以在URL中使用 ?& 来连接多个参数。

1
http://.../users?id=1&name=John

5. 接收前端数组

前端传对象数组

这里以JSONObject举例

Controller:

1
2
3
4
5
6
7
8
/**
* 更新二维码打印信息
*/
@PostMapping("/updateDevicePrintInfo")
public AjaxResult updatePrintInfo(@RequestBody JSONObject[] jsonObjectList) {
qrCodePrintService.updatePrintInfo(jsonObjectList);
return AjaxResult.success();
}

前端请求json:

1
2
3
4
[
{"name":"123","class":"1.2"},
{"name":"123","class":"1.2"}
]

前端传普通数组

Controller里面的参数列表是List<String>或者String[]都是一样的。

Controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* 删除redis数据,暂时放在端子的Controller中,将来再修改位置
*/
@PostMapping("/deleteRedisInfo")
public AjaxResult deleteRedis(@RequestBody List<String> resSpecIds) {
Set<String> keys = new HashSet<>();
//如果resSpecNames为空,则删除所有
if (resSpecIds == null || resSpecIds.isEmpty()) {
keys.addAll(redisService.redisTemplate.keys("RESSPEC:*"));
} else {
resSpecIds.forEach(resSpecId -> keys.addAll(redisService.redisTemplate.keys("RESSPEC:" + resSpecId + "_*")));
}

if (keys != null && !keys.isEmpty()) {
//执行删除操作
redisService.redisTemplate.delete(keys);
return AjaxResult.success("清除成功!");
} else {
return AjaxResult.success("未找到缓存数据!");
}

}

前端请求json:

1
[101210,101201]

6.PathVariable兼容数组

@PathVariable 注解可以用于处理 GET 和 POST 请求

@PathVariable可以接收数组和字符串类型的参数。

下面是一个示例:

1
2
3
4
5
6
7
8
9
@GetMapping("/example/{id}")
public void exampleMethod(@PathVariable String id) {
// 处理 id 参数
}

@PostMapping("/example/{ids}")
public void exampleMethod(@PathVariable String[] ids) {
// 处理 ids 参数
}
  • GET 请求,示例:/example/123
  • POST 请求,示例:/example/1,2,3

云海分页参数Page

项目默认的分页参数,需要放在params里面传,不能放在body里面传

Post不使用RequestBody注解

PostMapping如果不使用@RequesBody注解,接受的是方法体的还是url的?

接受的是Param中的值,不会去请求体中进行映射。

那么我们可以理解为如果使用@RequestBody注解,才会去请求体中搜索请求。不是用的话默认当做RequestParam注解。

2024-07-20新发现

或者是Body的form-data或者是Body的x-www-form-urlencoded也可以接收这种参数

如果前端传空字符串

“age”: “”,

如果前端在请求中加了这种属性:

  • 如果是String类型的字段会接收为空字符串
  • 如果不是字符串相关类型,比如数字,会接收为null

关于Controller请求的相关问题
http://wahoyu.xyz/2024/07/21/Controller2/
作者
Wahoyu
发布于
2024年7月21日
许可协议