java - 如何让 Spring 只看到添加到 url 的最后一个路径变量?
问题描述
我只想为 指定获取请求/show-owner/{id}
,但每次在 URL 中添加另一个路径时,我都会收到 404 not found 错误,我必须提供完整路径才能使其正常工作。
在这种情况下最好的方法是什么?
@GetMapping(value = {"/show-owner/{id}", "/show-item/show-owner/{id}",
"/show-user/user-items-table/show-item/show-owner/{id}",
"/show-user/user-items-table/show-item/show-owner/user-items-table/{id}",
"/show-owner/user-items-table/show-item/show-owner/{id}",
"/user-items-table/show-item/show-owner/{id}"})
public ModelAndView displayOwnerByItemId(@PathVariable String id) {
try {
UserDto user = userDtoMapper.toDto(itemService.getItemById(id).getOwner());
ModelAndView mav = new ModelAndView("show-user");
mav.addObject("user", user);
return mav;
} catch (ItemNotFound e) {
return new ModelAndView("redirect:/items");
}
}
解决方案
如果您只想要路径变量中的最后一个 id,您可以做一件事。您将以逗号分隔的字符串获取 id。所以最简单的解决方案是根据逗号分割字符串并像这样获取最后一个索引的值。
String k[] = id.split(",");
System.out.println(k[k.length-1]);//just to check whether u are getting the last id value or not
让我知道这是否有帮助。
推荐阅读
- intellij-idea - IntelliJ-添加testNG maven依赖带来junit
- angular - 在 mat-radio-group 中选择了多个 mat-radio-button
- c++ - Sizer 中的 C++ wxWidgets 巨大差距(初学者)
- javascript - 如何获取 Slack 操作消息发送的 JSON 有效负载?
- ubuntu - .NET Core 3.1 MSBuild 在 Ubuntu 上缺少 libc.musl-x86_64.so.1
- excel - Application.OnKey 用于多个打开的工作簿中的一个工作簿
- python - 如何在特定列中填写值?
- c# - 水晶报表在第二次运行时不显示数据
- deployment - 忽略注释的 Visual Studio 数据库项目
- java - 当通过反射调用 main 时,JVM 以可见的窗口退出