gpt4 book ai didi

java - @RequestParam 任意值

转载 作者:行者123 更新时间:2023-12-03 19:37:52 25 4
gpt4 key购买 nike

我的@Restcontroller 中有以下方法:

@GetMapping
public List<User> getByParameterOrAll(
@RequestParam(value = "email", required = false) String email,
@RequestParam(value = "phone", required = false) String phone) {

List<User> userList;
if ((email != null && !email.isEmpty()) && (phone == null || phone.isEmpty())) {
userList = super.getByEmail(email);
} else if ((email == null || email.isEmpty()) && (phone != null)) {
userList = super.getByPhone(phone);
} else {
userList = super.getAll();
}
return userList;
}

此方法允许处理以下 GET 请求:

GET:   /customers/
GET: /customers?email=emai@email.com
GET: /customers?phone=8-812-872-23-34

但如果有必要,可以为请求添加更多参数。如果它将是 10 或... 20 个参数,则上述方法的主体出现得离谱!如果有任何方法可以将 @RequestParam 的值传递给方法体,我可以实现,例如:

@GetMapping
public List<User> getByParameterOrAll(
@RequestParam(value = "any", required = false) String any) {

if (value=="email") {
userList = super.getByEmail(email);
} else if (value=="email") {
userList = super.getByPhone(email);
} else if .....
}

有没有办法在方法体中使用@RequestParam-value?

最佳答案

@RequestParam

When an @RequestParam annotation is declared as a Map or MultiValueMap, without a parameter name specified in the annotation, then the map is populated with the request parameter values for each given parameter name.

@GetMapping
public List<User> getByParameterOrAll(@RequestParam Map<String, String> parameters){
....
}

将为您提供所有参数和值作为 map 。

关于java - @RequestParam 任意值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60304684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com