parent
0ccd2f8b87
commit
a09f93768a
|
|
@ -115,9 +115,11 @@ public final class HttpRequestValues {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the full URL to use, if set.
|
* Return the URL to use.
|
||||||
* <p>This is mutually exclusive with {@link #getUriTemplate() uriTemplate}.
|
* <p>Typically, this comes from a {@link URI} method argument, which provides
|
||||||
* One of the two has a value but not both.
|
* the caller with the option to override the {@link #getUriTemplate()
|
||||||
|
* uriTemplate} from class and method {@code HttpExchange} annotations.
|
||||||
|
* annotation.
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public URI getUri() {
|
public URI getUri() {
|
||||||
|
|
@ -125,9 +127,8 @@ public final class HttpRequestValues {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the URL template for the request, if set.
|
* Return the URL template for the request. This comes from the values in
|
||||||
* <p>This is mutually exclusive with a {@linkplain #getUri() full URL}.
|
* class and method {@code HttpExchange} annotations.
|
||||||
* One of the two has a value but not both.
|
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public String getUriTemplate() {
|
public String getUriTemplate() {
|
||||||
|
|
@ -248,38 +249,29 @@ public final class HttpRequestValues {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the request URL as a full URL.
|
* Set the URL to use. When set, this overrides the
|
||||||
* <p>This is mutually exclusive with, and resets any previously set
|
* {@linkplain #setUriTemplate(String) URI template} from the
|
||||||
* {@linkplain #setUriTemplate(String) URI template} or
|
* {@code HttpExchange} annotation.
|
||||||
* {@linkplain #setUriVariable(String, String) URI variables}.
|
|
||||||
*/
|
*/
|
||||||
public Builder setUri(URI uri) {
|
public Builder setUri(URI uri) {
|
||||||
this.uri = uri;
|
this.uri = uri;
|
||||||
this.uriTemplate = null;
|
|
||||||
this.uriVars = null;
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the request URL as a String template.
|
* Set the request URL as a String template.
|
||||||
* <p>This is mutually exclusive with, and resets any previously set
|
|
||||||
* {@linkplain #setUri(URI) full URI}.
|
|
||||||
*/
|
*/
|
||||||
public Builder setUriTemplate(String uriTemplate) {
|
public Builder setUriTemplate(String uriTemplate) {
|
||||||
this.uriTemplate = uriTemplate;
|
this.uriTemplate = uriTemplate;
|
||||||
this.uri = null;
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a URI variable name-value pair.
|
* Add a URI variable name-value pair.
|
||||||
* <p>This is mutually exclusive with, and resets any previously set
|
|
||||||
* {@linkplain #setUri(URI) full URI}.
|
|
||||||
*/
|
*/
|
||||||
public Builder setUriVariable(String name, String value) {
|
public Builder setUriVariable(String name, String value) {
|
||||||
this.uriVars = (this.uriVars != null ? this.uriVars : new LinkedHashMap<>());
|
this.uriVars = (this.uriVars != null ? this.uriVars : new LinkedHashMap<>());
|
||||||
this.uriVars.put(name, value);
|
this.uriVars.put(name, value);
|
||||||
this.uri = null;
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -399,7 +391,7 @@ public final class HttpRequestValues {
|
||||||
public HttpRequestValues build() {
|
public HttpRequestValues build() {
|
||||||
|
|
||||||
URI uri = this.uri;
|
URI uri = this.uri;
|
||||||
String uriTemplate = (this.uriTemplate != null || uri != null ? this.uriTemplate : "");
|
String uriTemplate = (this.uriTemplate != null ? this.uriTemplate : "");
|
||||||
Map<String, String> uriVars = (this.uriVars != null ? new HashMap<>(this.uriVars) : Collections.emptyMap());
|
Map<String, String> uriVars = (this.uriVars != null ? new HashMap<>(this.uriVars) : Collections.emptyMap());
|
||||||
|
|
||||||
Object bodyValue = this.bodyValue;
|
Object bodyValue = this.bodyValue;
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,6 @@ public class UrlArgumentResolver implements HttpServiceArgumentResolver {
|
||||||
|
|
||||||
if (argument != null) {
|
if (argument != null) {
|
||||||
requestValues.setUri((URI) argument);
|
requestValues.setUri((URI) argument);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ import java.net.URI;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.web.service.annotation.GetExchange;
|
import org.springframework.web.service.annotation.GetExchange;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
@ -51,7 +52,7 @@ public class UrlArgumentResolverTests {
|
||||||
this.service.execute(dynamicUrl);
|
this.service.execute(dynamicUrl);
|
||||||
|
|
||||||
assertThat(getRequestValues().getUri()).isEqualTo(dynamicUrl);
|
assertThat(getRequestValues().getUri()).isEqualTo(dynamicUrl);
|
||||||
assertThat(getRequestValues().getUriTemplate()).isNull();
|
assertThat(getRequestValues().getUriTemplate()).isEqualTo("/path");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -67,7 +68,9 @@ public class UrlArgumentResolverTests {
|
||||||
@Test
|
@Test
|
||||||
void ignoreNull() {
|
void ignoreNull() {
|
||||||
this.service.execute(null);
|
this.service.execute(null);
|
||||||
|
|
||||||
assertThat(getRequestValues().getUri()).isNull();
|
assertThat(getRequestValues().getUri()).isNull();
|
||||||
|
assertThat(getRequestValues().getUriTemplate()).isEqualTo("/path");
|
||||||
}
|
}
|
||||||
|
|
||||||
private HttpRequestValues getRequestValues() {
|
private HttpRequestValues getRequestValues() {
|
||||||
|
|
@ -78,7 +81,7 @@ public class UrlArgumentResolverTests {
|
||||||
private interface Service {
|
private interface Service {
|
||||||
|
|
||||||
@GetExchange("/path")
|
@GetExchange("/path")
|
||||||
void execute(URI uri);
|
void execute(@Nullable URI uri);
|
||||||
|
|
||||||
@GetExchange
|
@GetExchange
|
||||||
void executeNotUri(String other);
|
void executeNotUri(String other);
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ package org.springframework.web.reactive.function.client.support;
|
||||||
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -31,6 +32,8 @@ import org.junit.jupiter.api.Test;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
import reactor.test.StepVerifier;
|
import reactor.test.StepVerifier;
|
||||||
|
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestAttribute;
|
import org.springframework.web.bind.annotation.RequestAttribute;
|
||||||
import org.springframework.web.reactive.function.client.WebClient;
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
import org.springframework.web.service.annotation.GetExchange;
|
import org.springframework.web.service.annotation.GetExchange;
|
||||||
|
|
@ -77,7 +80,7 @@ public class WebClientHttpServiceProxyTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void greetingWithRequestAttribute() throws Exception {
|
void greetingWithRequestAttribute() {
|
||||||
|
|
||||||
Map<String, Object> attributes = new HashMap<>();
|
Map<String, Object> attributes = new HashMap<>();
|
||||||
|
|
||||||
|
|
@ -100,7 +103,19 @@ public class WebClientHttpServiceProxyTests {
|
||||||
assertThat(attributes).containsEntry("myAttribute", "myAttributeValue");
|
assertThat(attributes).containsEntry("myAttribute", "myAttributeValue");
|
||||||
}
|
}
|
||||||
|
|
||||||
private TestHttpService initHttpService() throws Exception {
|
@Test // gh-29624
|
||||||
|
void uri() throws Exception {
|
||||||
|
String expectedBody = "hello";
|
||||||
|
prepareResponse(response -> response.setResponseCode(200).setBody(expectedBody));
|
||||||
|
|
||||||
|
URI dynamicUri = this.server.url("/greeting/123").uri();
|
||||||
|
String actualBody = initHttpService().getGreetingById(dynamicUri, "456");
|
||||||
|
|
||||||
|
assertThat(actualBody).isEqualTo(expectedBody);
|
||||||
|
assertThat(this.server.takeRequest().getRequestUrl().uri()).isEqualTo(dynamicUri);
|
||||||
|
}
|
||||||
|
|
||||||
|
private TestHttpService initHttpService() {
|
||||||
WebClient webClient = WebClient.builder().baseUrl(this.server.url("/").toString()).build();
|
WebClient webClient = WebClient.builder().baseUrl(this.server.url("/").toString()).build();
|
||||||
return initHttpService(webClient);
|
return initHttpService(webClient);
|
||||||
}
|
}
|
||||||
|
|
@ -127,7 +142,9 @@ public class WebClientHttpServiceProxyTests {
|
||||||
@GetExchange("/greeting")
|
@GetExchange("/greeting")
|
||||||
Mono<String> getGreetingWithAttribute(@RequestAttribute String myAttribute);
|
Mono<String> getGreetingWithAttribute(@RequestAttribute String myAttribute);
|
||||||
|
|
||||||
|
@GetExchange("/greetings/{id}")
|
||||||
|
String getGreetingById(@Nullable URI uri, @PathVariable String id);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue