java - @PathVariable 不适用于非拉丁语言
问题描述
我有一个从数据库中获取 Post 的控制器方法。
@RequestMapping(value = {"/post/{id}", "/post/{id}/{title}"}, method = RequestMethod.GET)
post 表在数据库中如下所示:
- ID
- 标题
- 描述
- 蛞蝓
当我收到从数据库获取帖子的 GET 请求时,我从数据库中获取帖子并检查 URL 中的标题 slug 是否与数据库中的 slug 匹配,如果它们相同,我将显示它,如果不是,我会将请求重定向到与数据库中的 slug 相同的方法。
我尝试了以下代码
@RequestMapping(value = {"/post/{id}", "/post/{id}/{title}"}, method = RequestMethod.GET)
public String getPost(@PathVariable("id") Long id,
@PathVariable("title") Optional<String> title,
Model model,
HttpServletRequest request) {
Optional<PostDTO> postDTO = postService.findOne(id);
if(postDTO.isPresent()) {
String slug = postDTO.get().getSlug();
model.addAttribute("post", postDTO.get());
if( (title.isPresent() && !slug.equals(title.get())) || !title.isPresent()) {
// redirect
return "redirect:/post/" + postDTO.get().getId() + "/" + slug;
} else {
return "post/index";
}
} else {
return "post/not_found";
}
}
但是当它到达重定向语句时,发送了 id,但标题为空,所以我得到一个无限循环。
我做错了什么?
更新:
经过一些调试,我发现如果存储在 slug 变量中的文本是英语,我的代码工作正常,但如果它是阿拉伯语,它将为空。
return "redirect:/post/" + postDTO.get().getId() + "/" + slug;
return "redirect:/post/" + postDTO.get().getId() + "/" + "Hello"; // working fine
return "redirect:/post/" + postDTO.get().getId() + "/" + "مرحبا"; // not working
不工作意味着@PathVariable("title") Optional<String> title
重定向后将为空。
解决方案
推荐阅读
- angular - 构建应用程序时未找到/注册错误的 Angular Service Worker
- powershell - 导出 Pfxcertificate 命令在 Powershell 中不可用
- html - 选择字段中的选项仅在切换选项卡后出现
- c# - 如果添加到 where 子句的参数为空,则从 db 获取所有行
- python - 为什么 numpy.nextafter(0., 1.) != numpy.finfo(float).tiny?
- smartgit - SmartGit 18.2 preview 8 #13132 兼容 Mac OSX Mojave 10.14
- amazon-web-services - 无法通过 Terraform 在 cloudwatch 警报中使用单位
- python - 将 Dropout 添加到测试/推理阶段
- excel - 如何在“Application.EnableEvents”属性的更改上放置触发器
- angular - Angular Material mat-option 替代检索值