有没有办法在@Path注释中指定查询参数



我有一个Quarkus服务,我正在尝试(在某种程度上(模仿S3 API。

我注意到S3中的多部分上传是这样的:

POST bucket.host.com /KEY?uploads // signifies that a multipart upload on KEY is starting
// and returns an uploadId for it
PUT bucket.host.com /KEY?uploadId=UPLOADID&partNumber=2 // uploads part 2 of the file,
// content is in request body
PUT bucket.host.com /KEY?uploadId=UPLOADID&partNumber=1 // upload part 1
POST bucket.host.com /KEY?uploadId=UPLOADID // signifies that the multipart upload is completed

一个";原子";文件上传(也就是说,没有多部分,文件只是一次性上传的(";重复使用";PUT路径,所以要以这种方式上传文件,只需对URL执行PUT并跳过两个查询参数。

我不会用";bucket被嵌入到主机名"中;方法,而它将成为请求URL的一部分。

因此,我目前拥有的是一个资源类,它看起来是这样的(在我的情况下,简化后的bucket被称为"项目",KEY被称为"路径"(:

@Path("/fts")
class FileTransferService(
@ConfigProperty(name = "fts.project.root")
private val projectRoot: String
) {
@POST
@Path("{project}/{path:.*}")
@Produces(MediaType.APPLICATION_JSON)
fun multipartFileUpload(
@PathParam project: String,
@PathParam path: String,
@QueryParam("uploads") uploads: String?,
@QueryParam("uploadId") uploadId: String?
): String {
// Here I'll have to check whether uploads or uploadId is set
// and determine the path to take with some if/else statements.
}
@PUT
@Path("{project}/{path:.*}")
@Produces(MediaType.APPLICATION_JSON)
fun upload(
@PathParam project: String,
@PathParam path: String,
@QueryParam("uploadId") uploadId: String?,
@QueryParam("partNumber") partNumber: String?,
body: InputStream
): String {
// Here I'll need to check uploadId and/or partNumber to determine
// whether this is an "atomic file upload" or a single part of a 
// multi part upload and determine the path to take with if/else again.
}
}

正如您在上面代码的注释中看到的那样,我必须进行一些if/else检查,并根据是否设置了查询参数来执行不同的路线。

这让我很恼火。我宁愿有不同的@POST方法,一种是捕捉设置了uploads参数的情况,另一种是设置了uploadId参数的情况。

我尝试过使用多个方法,其中一个指定QueryParameter,另一个不指定,但似乎不起作用。我在quarkus日志中收到一条信息消息,说为该路径找到了多个匹配项,其中一个是随机选择的(可能只是第一个(。

所以我猜,如果这是可能的,那就是以某种方式将查询参数放入@Path注释中,但我还没能找到如何做到这一点。一个可能的原因是这是不可能的,但我想在这里询问并得到确认。

如果您想使用查询参数,那么当前版本是正确的。

您将无法使用JAX-RS做您想做的事情。路径决定了调用的方法,查询参数只是可选的附加信息。

如果您绝对希望根据参数的存在情况使用不同的方法,则需要使用路径参数。

最新更新