PathVariable 中的正则表达式无法正常工作 Spring

Regexp in PathVariable does not work correctly Spring

有控制器。

我提出请求 http://localhost:8080/api/v1/download/avatar/1/GtAHWk3EVjBcltY.JPG - 我得到了 404。它没有达到方法。为什么?常规赛有什么问题?

我使用 Spring Boot 2。但问题是有一个项目,没有引导并且有相同的映射 - 一切正常。

@Controller
@RequestMapping("/api/v1/download")
public class DownloadRestController {

    private final DownloadService downloadService;

    @Autowired
    public DownloadRestController(DownloadService downloadService) {
        this.downloadService = downloadService;
    }

    @RequestMapping(value = "/avatar/{path:.*}")
    public void download(HttpServletRequest request, @PathVariable String path) {
        this.downloadService.download(request, "/avatar/"+path);
    }

}

您正在呼叫

http://localhost:8080/api/v1/download/avatar/1/GtAHWk3EVjBcltY.JPG

并且您的方法接受 http://localhost:8080/api/v1/download/avatar/{path}

所以你传递了两个 @PathVariables - 这就是你得到 HTTP-404 的原因。 你必须决定是否要

@PathVariable("id") long id, @PathVariable("path") String path

有 2 个路径变量 或者

@PathVariable("path") String path 

只有一个。

此外,除了上面的内容之外,您不需要添加一些类似正则表达式的内容。

@PathVariable("path") String path 

你的论据应该很有效。