Remove the few remaining usages of UriTemplate
Also update Javadoc of UriTemplate to point to UriComponentsBuilder and UriBuilderFactory as more flexible options. See gh-24094
This commit is contained in:
@@ -44,9 +44,13 @@ import org.springframework.util.ObjectUtils;
|
||||
* </pre>
|
||||
*
|
||||
* <p>If you would like to provide a URI template with variables, consider using
|
||||
* {@link org.springframework.web.util.UriTemplate}:
|
||||
* {@link org.springframework.web.util.DefaultUriBuilderFactory DefaultUriBuilderFactory}:
|
||||
* <pre class="code">
|
||||
* URI uri = new UriTemplate("https://example.com/{foo}").expand("bar");
|
||||
* // Create shared factory
|
||||
* UriBuilderFactory factory = new DefaultUriBuilderFactory();
|
||||
*
|
||||
* // Use factory to create URL from template
|
||||
* URI uri = factory.uriString("https://example.com/{foo}").build("bar");
|
||||
* RequestEntity<MyRequest> request = RequestEntity.post(uri).accept(MediaType.APPLICATION_JSON).body(body);
|
||||
* </pre>
|
||||
*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -30,14 +30,19 @@ import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Represents a URI template. A URI template is a URI-like String that contains variables
|
||||
* enclosed by braces ({@code {}}) which can be expanded to produce an actual URI.
|
||||
* Representation of a URI template that can be expanded with URI variables via
|
||||
* {@link #expand(Map)}, {@link #expand(Object[])}, or matched to a URL via
|
||||
* {@link #match(String)}. This class is designed to be thread-safe and
|
||||
* reusable, and allows any number of expand or match calls.
|
||||
*
|
||||
* <p>See {@link #expand(Map)}, {@link #expand(Object[])}, and {@link #match(String)}
|
||||
* for example usages.
|
||||
*
|
||||
* <p>This class is designed to be thread-safe and reusable, allowing for any number
|
||||
* of expand or match calls.
|
||||
* <p><strong>Note:</strong> this class uses {@link UriComponentsBuilder}
|
||||
* internally to expand URI templates, and is merely a shortcut for already
|
||||
* prepared URI templates. For more dynamic preparation and extra flexibility,
|
||||
* e.g. around URI encoding, consider using {@code UriComponentsBuilder} or the
|
||||
* higher level {@link DefaultUriBuilderFactory} which adds several encoding
|
||||
* modes on top of {@code UriComponentsBuilder}. See the
|
||||
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-uri-building">reference docs</a>
|
||||
* for further details.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Juergen Hoeller
|
||||
|
||||
@@ -27,7 +27,7 @@ import java.util.Map;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.web.util.UriTemplate;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -58,7 +58,7 @@ public class RequestEntityTests {
|
||||
|
||||
@Test
|
||||
public void uriVariablesExpansion() throws URISyntaxException {
|
||||
URI uri = new UriTemplate("https://example.com/{foo}").expand("bar");
|
||||
URI uri = UriComponentsBuilder.fromUriString("https://example.com/{foo}").buildAndExpand("bar").toUri();
|
||||
RequestEntity.get(uri).accept(MediaType.TEXT_PLAIN).build();
|
||||
|
||||
String url = "https://www.{host}.com/{path}";
|
||||
@@ -66,7 +66,7 @@ public class RequestEntityTests {
|
||||
String path = "foo/bar";
|
||||
URI expected = new URI("https://www.example.com/foo/bar");
|
||||
|
||||
uri = new UriTemplate(url).expand(host, path);
|
||||
uri = UriComponentsBuilder.fromUriString(url).buildAndExpand(host, path).toUri();
|
||||
RequestEntity<?> entity = RequestEntity.get(uri).build();
|
||||
assertThat(entity.getUrl()).isEqualTo(expected);
|
||||
|
||||
@@ -74,7 +74,7 @@ public class RequestEntityTests {
|
||||
uriVariables.put("host", host);
|
||||
uriVariables.put("path", path);
|
||||
|
||||
uri = new UriTemplate(url).expand(uriVariables);
|
||||
uri = UriComponentsBuilder.fromUriString(url).buildAndExpand(uriVariables).toUri();
|
||||
entity = RequestEntity.get(uri).build();
|
||||
assertThat(entity.getUrl()).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -37,7 +37,7 @@ import org.springframework.validation.Errors;
|
||||
import org.springframework.web.bind.EscapedErrors;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.util.HtmlUtils;
|
||||
import org.springframework.web.util.UriTemplate;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
/**
|
||||
* Context holder for request-specific state, like the {@link MessageSource} to
|
||||
@@ -218,8 +218,7 @@ public class RequestContext {
|
||||
*/
|
||||
public String getContextUrl(String relativeUrl, Map<String, ?> params) {
|
||||
String url = StringUtils.applyRelativePath(getContextPath() + "/", relativeUrl);
|
||||
UriTemplate template = new UriTemplate(url);
|
||||
url = template.expand(params).toASCIIString();
|
||||
url = UriComponentsBuilder.fromUriString(url).buildAndExpand(params).encode().toUri().toASCIIString();
|
||||
return getExchange().transformUrl(url);
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,8 @@ import java.util.Map;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.util.UriTemplate;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
/**
|
||||
* Dummy request context used for FreeMarker macro tests.
|
||||
@@ -107,8 +108,8 @@ public class DummyMacroRequestContext {
|
||||
* @see org.springframework.web.reactive.result.view.RequestContext#getContextUrl(String, Map)
|
||||
*/
|
||||
public String getContextUrl(String relativeUrl, Map<String,String> params) {
|
||||
UriTemplate template = new UriTemplate(relativeUrl);
|
||||
return getContextPath() + template.expand(params).toASCIIString();
|
||||
UriComponents uric = UriComponentsBuilder.fromUriString(relativeUrl).buildAndExpand(params);
|
||||
return getContextPath() + uric.toUri().toASCIIString();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -50,7 +50,7 @@ import org.springframework.web.servlet.LocaleContextResolver;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
import org.springframework.web.servlet.ThemeResolver;
|
||||
import org.springframework.web.util.HtmlUtils;
|
||||
import org.springframework.web.util.UriTemplate;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
import org.springframework.web.util.WebUtils;
|
||||
|
||||
@@ -563,8 +563,7 @@ public class RequestContext {
|
||||
*/
|
||||
public String getContextUrl(String relativeUrl, Map<String, ?> params) {
|
||||
String url = getContextPath() + relativeUrl;
|
||||
UriTemplate template = new UriTemplate(url);
|
||||
url = template.expand(params).toASCIIString();
|
||||
url = UriComponentsBuilder.fromUriString(url).buildAndExpand(params).encode().toUri().toASCIIString();
|
||||
if (this.response != null) {
|
||||
url = this.response.encodeURL(url);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -23,7 +23,8 @@ import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.web.servlet.support.BindStatus;
|
||||
import org.springframework.web.servlet.support.RequestContext;
|
||||
import org.springframework.web.util.UriTemplate;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
/**
|
||||
* Dummy request context used for FreeMarker macro tests.
|
||||
@@ -140,8 +141,8 @@ public class DummyMacroRequestContext {
|
||||
* @see org.springframework.web.servlet.support.RequestContext#getContextUrl(String, Map)
|
||||
*/
|
||||
public String getContextUrl(String relativeUrl, Map<String,String> params) {
|
||||
UriTemplate template = new UriTemplate(relativeUrl);
|
||||
return getContextPath() + template.expand(params).toASCIIString();
|
||||
UriComponents uric = UriComponentsBuilder.fromUriString(relativeUrl).buildAndExpand(params);
|
||||
return getContextPath() + uric.toUri().toASCIIString();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user