Skip to content

Provide convenient ways to specify query parameters to WebClient [SPR-15124] #19691

@spring-projects-issues

Description

@spring-projects-issues

Toshiaki Maki opened SPR-15124 and commented

In my understanding, when specifying query parameters to WebClient, ClientRequest#method(HttpMethod, URI) or building url string can be used.
I'd like to have more convenient way.

For example, when http://api.example.com is a externalized property(api.path) and an application accesses $(api.path)/v1/foo?bar=$(bar)&baz=$(baz), we need to write code such as following:

int bar = 100;
String baz = aaa;
ClientRequest<Void> req = ClientRequest
		.method(GET, UriComponentsBuilder.fromHttpUrl(apiPath)
		  .pathSegment("v1", "foo")
		  .queryParam("bar", bar)
		  .queryParam("baz", baz).build().encode().toUri());

// or ClientRequest<Void> req = ClientRequest.GET(apiPath + "/v1/foo?bar=" + bar + "&baz=" + baz);  // I don't like this style

I would like to propose adding the following methods in org.springframework.web.reactive.function.client.ClientRequest

// though rough design
static BodyBuilder method(HttpMethod method, String url,
		Function<UriComponentsBuilder, UriComponents> f) {
	UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
	URI uri = f.apply(builder).toUri();
	return new DefaultClientRequestBuilder(method, uri);
}

static HeadersBuilder<?> GET(String url,
		Function<UriComponentsBuilder, UriComponents> f) {
	return method(HttpMethod.GET, url, f);
}

// POST, PUT, ...

With this method, the example code above can be re-written as follows:

ClientRequest<Void> req = ClientRequest
		.GET(apiHost, b -> b.pathSegment("v1", "foo")
                                                 .queryParam("bar", bar)
                                                 .queryParam("baz", baz)
                                                 .build().encode())
		.build();

Affects: 5.0 M4

Issue Links:

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions