Skip to content

Add support for POJO to query string mapping in Spring HTTP Interface #35381

@csh0034

Description

@csh0034

Description
I would like to request a feature that allows passing a POJO as query string in Spring’s HTTP Interface

Currently, while migrating from OpenFeign, I noticed that Spring does not provide an equivalent to Feign’s @QueryMap. Because of this limitation, I had to implement my own HttpServiceArgumentResolver to achieve the same behavior.

Reference from Feign documentation:
https://docs.spring.io/spring-cloud-openfeign/reference/spring-cloud-openfeign.html#feign-querymap-support

This request is very similar to #34227.

Could this functionality be added as a built-in feature in Spring? Alternatively, I can prepare a PR if that would help

Custom Implementation

@Target(AnnotationTarget.VALUE_PARAMETER)
@Retention(AnnotationRetention.RUNTIME)
annotation class QueryMap

class QueryMapHttpServiceArgumentResolver : HttpServiceArgumentResolver {
  override fun resolve(argument: Any?, parameter: MethodParameter, requestValues: HttpRequestValues.Builder): Boolean {
    if (!parameter.hasParameterAnnotation(QueryMap::class.java)) return false
    requireNotNull(argument) { "argument cannot be null" }

    for (entry in argument.toMap()) {
      requestValues.addRequestParameter(entry.key, entry.value)
    }

    return true
  }

  private fun Any.toMap(): Map<String, String?> {
    return (this::class as KClass<Any>).memberProperties.associate { prop ->
      prop.name to prop.get(this)?.toString()
    }
  }
}

interface TestClient {
  @GetExchange("/test")
  fun queryObject(@QueryMap dto: TestDto): String
}

data class TestDto(
  val id: Long, 
  val name: String,
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: duplicateA duplicate of another issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions