You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

javax.ws.rs.client调用Spring接口如何同时传@RequestParam和@RequestBody

实现方法

@RequestParam 对应的参数属于URL查询参数,不会放在POST请求体中,不需要修改你现有传递@RequestBody的逻辑,只需要在构建请求时把查询参数附加到URL上即可。

RESTEasy(JAX-RS)客户端支持两种传参方式:

  • 推荐方式:调用queryParam方法绑定参数,自动处理URL转义,不需要手动拼接字符串
// 替换为你实际的projectId值
val targetProjectId: UUID = UUID.fromString("xxx-xxx-xxx-xxx")
val testResponse = this.resteasyClient
    .target("http://localhost:$port$relativePath")
    // 绑定@RequestParam对应的projectId参数
    .queryParam("projectId", targetProjectId)
    .request()
    .header("Authorization", "Bearer $accessToken")
    // 原有传递请求体的逻辑保持不变
    .post(Entity.json(requestBody))
  • 手动拼接URL方式:直接将查询参数拼接到URL末尾,参数跟在?后,多个参数用&分隔
val targetProjectId: UUID = UUID.fromString("xxx-xxx-xxx-xxx")
// 直接拼接参数到请求路径
val requestUrl = "http://localhost:$port$relativePath?projectId=$targetProjectId"
val testResponse = this.resteasyClient
    .target(requestUrl)
    .request()
    .header("Authorization", "Bearer $accessToken")
    .post(Entity.json(requestBody))

注意:不要把@RequestParam参数放到请求体JSON里,也不要用表单提交格式传参。Spring会自动从URL查询串解析projectId参数,同时从请求体解析JSON到RegulationTraceFilter对象,两个参数的解析逻辑互不冲突。

内容的提问来源于stack exchange,提问作者Ekaterina Ivanova iceja.net

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 05:36:22