gpt4 book ai didi

java - Spring 请求映射与正则表达式如 javax.ws.rs

转载 作者:行者123 更新时间:2023-12-01 09:42:05 24 4
gpt4 key购买 nike

我正在尝试重写 this Google App Engine maven server repository到 Spring 。

我有 URL 映射问题。
Maven repo 服务器标准如下所示:

  • 以斜线结尾的 URL 指向一个文件夹,例如:
    http://127.0.0.1/testDir/
    http://127.0.0.1/testDir/testDir2/
  • 所有其他(末尾没有斜线)指向文件,例如:
    http://127.0.0.1/testFile.jar
    http://127.0.0.1/testFile.jar.sha1
    http://127.0.0.1/testDir/testFile2.pom
    http://127.0.0.1/testDir/testFile2.pom.md5

  • directories 的原始应用程序映射和 files .

    有使用注释 @javax.ws.rs.Path与 Spring 不同,它支持 regxy。

    我尝试了一堆组合,例如这样的:

    @ResponseBody
    @GetMapping("/{file: .*}")
    public String test1(@PathVariable String file) {
    return "test1 " + file;
    }

    @ResponseBody
    @GetMapping("{dir: .*[/]{1}$}")
    public String test2(@PathVariable String dir) {
    return "test2 " + dir;
    }

    但是我不知道如何在 Spring 应用程序中以正确的方式做到这一点。

    我想避免编写自定义 servlet 调度程序。

    最佳答案

    我曾经遇到过类似的问题,也是关于 Maven 端点的 Spring 实现。

    对于文件端点,您可以执行以下操作

    /**
    * An example Maven endpoint for Jar files
    */
    @GetMapping("/**/{artifactId}/{version}/{artifactId}-{version}.jar")
    public ResponseEntity<String> getJar(@PathVariable("artifactId") String artifactId, @PathVariable("version") String version) {
    ...
    }

    这为您提供了 artifactIdversion ,但对于 groupId你需要做一些字符串解析。您可以获得当前的 requestUriServletUriComponentsBuilder 的帮助下

    String requestUri = ServletUriComponentsBuilder.fromCurrentRequestUri().build().toUri().toString();
    // requestUri = /api/v1/com/my/groupId/an/artifact/v1/an-artifact-v1.jar

    对于文件夹端点,我不确定这是否可行,但您可以尝试一下

    @GetMapping("/**/{artifactId}/{version}")
    public ResponseEntity<String> getJar(@PathVariable("artifactId") String artifactId, @PathVariable("version") String version) {
    // groupId extracted as before from the requestUri
    ...
    }

    关于java - Spring 请求映射与正则表达式如 javax.ws.rs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61017815/

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