Quarkus 3.10.2 Rest Client报错:仅允许单个Body参数
问题解决:Quarkus 3.10.2升级后REST Client报错“can only have a single body parameter: token”
问题原因
Quarkus 3.10.2升级了MicroProfile Rest Client的底层实现,对REST客户端方法的参数校验规则更严格:
- GET请求不允许存在body参数,所有参数必须明确标注对应的参数类型注解(如
@PathParam、@QueryParam、@HeaderParam) - 你的代码中
Set<String> pc没有添加任何参数注解,被框架默认识别为body参数;同时@PathParam("userId")对应的路径/aa/productNames里没有{userId}占位符,注解无效,进一步触发参数解析异常
解决方案
- 给未注解的参数
pc添加正确的参数类型注解(根据实际接口需求选择,以下以查询参数为例) - 修正
userId的参数注解:要么在路径中添加{userId}占位符,要么改为@QueryParam/@HeaderParam - 移除
token上的@NotBody注解(@ClientHeaderParam引用的参数会被自动识别为非body参数,无需额外标注)
修改后的代码示例
import io.quarkus.vertx.web.Body; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.QueryParam; import org.eclipse.microprofile.rest.client.annotation.ClientHeaderParam; import org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders; import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; import java.util.List; import java.util.Set; @RegisterRestClient(configKey = "aa-api") @RegisterClientHeaders(RequestUUIDHeaderFactory.class) public interface HttpProductService { @GET // 如果userId是路径参数,就改成@Path("/aa/productNames/{userId}") @Path("/aa/productNames") @ClientHeaderParam(name = "Authorization", value = "Bearer {token}") List<PDto> getProductNames(@QueryParam("userId") String uid, String token, @QueryParam("pc") Set<String> pc); }
额外说明
如果pc需要以多值查询参数的形式传递(如?pc=val1&pc=val2),Quarkus REST Client会自动将Set<String>类型的参数解析为多值查询参数,无需额外配置。
内容的提问来源于stack exchange,提问作者user25234022
相关产品推荐
相关产品推荐

