diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeMethodArgumentResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeMethodArgumentResolver.java index a705a3f422f..953f1a0dcbe 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeMethodArgumentResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeMethodArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -111,7 +111,8 @@ public class ServerWebExchangeMethodArgumentResolver extends HandlerMethodArgume } else if (UriBuilder.class == paramType || UriComponentsBuilder.class == paramType) { URI uri = exchange.getRequest().getURI(); - return UriComponentsBuilder.fromUri(uri).replacePath(null).replaceQuery(null); + String contextPath = exchange.getRequest().getPath().contextPath().value(); + return UriComponentsBuilder.fromUri(uri).replacePath(contextPath).replaceQuery(null); } else { // should never happen... diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeMethodArgumentResolverTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeMethodArgumentResolverTests.java index 7f0539fbbca..300e5139e04 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeMethodArgumentResolverTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeMethodArgumentResolverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -97,9 +97,23 @@ public class ServerWebExchangeMethodArgumentResolverTests { assertThat(value).isNotNull(); assertThat(value.getClass()).isEqualTo(UriComponentsBuilder.class); - assertThat(((UriComponentsBuilder) value).path("/next").toUriString()).isEqualTo("https://example.org:9999/next"); + assertThat(((UriComponentsBuilder) value).path("/next").toUriString()) + .isEqualTo("https://example.org:9999/next"); } + @Test // gh-25822 + public void resolveUriComponentsBuilderWithContextPath() { + ServerWebExchange exchange = MockServerWebExchange.from( + MockServerHttpRequest.get("https://example.org:9999/app/path?q=foo").contextPath("/app")); + + MethodParameter param = this.testMethod.arg(UriComponentsBuilder.class); + Object value = this.resolver.resolveArgument(param, new BindingContext(), exchange).block(); + + assertThat(value).isNotNull(); + assertThat(value.getClass()).isEqualTo(UriComponentsBuilder.class); + assertThat(((UriComponentsBuilder) value).path("/next").toUriString()) + .isEqualTo("https://example.org:9999/app/next"); + } @SuppressWarnings("unused") diff --git a/src/docs/asciidoc/web/webflux.adoc b/src/docs/asciidoc/web/webflux.adoc index 584ea2eb38f..c3e0d2cf346 100644 --- a/src/docs/asciidoc/web/webflux.adoc +++ b/src/docs/asciidoc/web/webflux.adoc @@ -1903,8 +1903,8 @@ and others) and is equivalent to `required=false`. See <> for more details. | `UriComponentsBuilder` -| For preparing a URL relative to the current request's host, port, scheme, and path. - See <>. +| For preparing a URL relative to the current request's host, port, scheme, and + context path. See <>. | `@SessionAttribute` | For access to any session attribute -- in contrast to model attributes stored in the session diff --git a/src/docs/asciidoc/web/webmvc.adoc b/src/docs/asciidoc/web/webmvc.adoc index 38608acedc4..4542ad9d981 100644 --- a/src/docs/asciidoc/web/webmvc.adoc +++ b/src/docs/asciidoc/web/webmvc.adoc @@ -2085,7 +2085,7 @@ and others) and is equivalent to `required=false`. | `UriComponentsBuilder` | For preparing a URL relative to the current request's host, port, scheme, context path, and - the literal part of the servlet mapping. See <>. + the literal part of the servlet mapping. See <>. | `@SessionAttribute` | For access to any session attribute, in contrast to model attributes stored in the session