本文介绍了Resteasy可以查看JAX-RS方法的参数类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在JAX-RS Web服务中使用Resteasy 3.0.9,最近切换到3.0.19,开始看到很多RESTEASY002142: Multiple resource methods match request警告.

We were using Resteasy 3.0.9 for our JAX-RS webservices, and recently switched to 3.0.19, where we started to see a lot of RESTEASY002142: Multiple resource methods match request warnings.

例如,我们有类似的方法:

For example, we have methods like:

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

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

我不确定它在3.0.9中是如何工作的,可能是,我们很幸运,因为Resteasy似乎从所有候选方法中选择了第一种方法(以及3.0.19种候选方法).

I'm not sure how it worked in 3.0.9, probably, we just were very lucky as Resteasy seems to select first method from all candidates (and 3.0.19 sorts candidate methods).

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

One solution is to explicitly specify regex: @Path("/{id : [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}}")

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

But is there a way to somehow tell Resteasy to look into method parameters and construct appropriate regex automatically?

推荐答案

据我所知,RESTEasy在匹配请求时不会考虑方法参数类型.根据 JSR-339 (由RESTEasy实现),这就是请求匹配的方式流程有效:

As far as I know, RESTEasy won't take the method parameter type into consideration when matching a request. According the JSR-339 (that RESTEasy implements), this is how the request matching process works:

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

The JAX-RS implementations must match the requested URI with the @Path annotation values. In the @Path annotation value you can define variables, that are denoted by braces ({ and }).

作为请求匹配的一部分,JAX-RS实现将使用指定的正则表达式([ˆ/]+?)(如果未指定正则表达式)替换每个URI模板变量.

As part of the request matching, the JAX-RS implementation will replace each URI template variable with the specified regular expression or ([ˆ/]+?) if no regular expression is specified.

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

To address the situation you mentioned in your question, you should specify a regex to match UUIDs on one resource method:

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

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

And you also may consider a regex to match integers on the other resource method:

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

这篇关于Resteasy可以查看JAX-RS方法的参数类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 22:24