gpt4 book ai didi

java - Resteasy 可以查看 JAX-RS 方法的参数类型吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:05:04 25 4
gpt4 key购买 nike

我们为我们的 JAX-RS 网络服务使用 Resteasy 3.0.9,最近切换到 3.0.19,我们开始看到很多 RESTEASY002142: Multiple resource methods match request 警告。

例如,我们有这样的方法:

@Path("/{id}") 
public String getSome(UUID id)

@Path("/{id}")
public String getSome(int id)

我不确定它在 3.0.9 中是如何工作的,可能我们只是非常幸运,因为 Resteasy 似乎从所有候选方法(以及 3.0.19 排序候选方法)中选择第一个方法。

一种解决方案是显式指定正则表达式:@Path("/{id : [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f ]{4}-[0-9a-f]{4}-[0-9a-f]{12}}")

但是有没有办法以某种方式告诉 Resteasy 查看方法参数并自动构造适当的正则表达式?

最佳答案

据我所知,RESTEasy 在匹配请求时不会考虑方法参数类型。根据JSR-339 (RESTEasy 实现),这是请求匹配过程的工作原理:

A request is matched to the corresponding resource method or sub-resource method by comparing the normalized request URI, the media type of any request entity, and the requested response entity format to the metadata annotations on the resource classes and their methods. If no matching resource method or sub-resource method can be found then an appropriate error response is returned. [...]

JAX-RS 实现必须将请求的 URI 与 @Path 匹配注释值。在@Path annotation value 您可以定义变量,用大括号({})表示。

作为请求匹配的一部分,如果没有指定正则表达式。

要解决您在问题中提到的情况,您应该指定一个正则表达式来匹配一种资源方法上的 UUID:

@Path("{id : [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}}")

您还可以考虑使用正则表达式来匹配其他资源方法上的整数:

@Path("{id : \\d+}")

关于java - Resteasy 可以查看 JAX-RS 方法的参数类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39635174/

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