java - 在 Spring Boot RestController 中区分带有查询参数的端点与没有查询参数的端点
问题描述
如何区分具有查询参数的端点与休息控制器中没有查询参数的端点?下面的映射会引发此错误 -
引起:java.lang.IllegalStateException:不明确的映射。无法映射“itemController”方法
@GetMapping("/")
@ResponseStatus(HttpStatus.OK)
public List<Item> getAllItems(){
return menuService.getAllItems();
}
@GetMapping("/")
@ResponseStatus(HttpStatus.OK)
public List<Item> getAllItems(@RequestParam("itemtype") ItemType itemType){
return menuService.findItemsByItemType(itemType);
}
解决方案
使参数可选:
@GetMapping("/")
@ResponseStatus(HttpStatus.OK)
public List<Item> getAllItems(@RequestParam(name = "itemtype", required = false) ItemType itemType){
if (itemType == null)
return menuService.getAllItems();
return menuService.findItemsByItemType(itemType);
}
推荐阅读
- powershell - PowerShell 2 等效于 Enable-NetAdapterBinding
- node.js - 拦截任何 http.IncomingMessage 并打印正文
- jquery - Bootstrap 模态数据刷新
- php - 为什么身份验证令牌不与 CURL 一起显示?
- apache-spark-sql - pyspark to_timestamp() 返回空值
- xpath - 导入到 Google 表格第一次/第二次/第三次出现在网站上的课程
- android - 自定义搜索栏
- python - 在熊猫数据框中搜索字符串
- azure - 构建、分析和崩溃报告 - 应用中心或 Azure
- r - 在嵌套列中保留原始类型“tibble”