Skip to content

Commit 9821dad

Browse files
committed
Merge branch '4.0.x' into 4.1.x
Closes gh-51121
2 parents 8adbbd7 + b9ebff0 commit 9821dad

24 files changed

Lines changed: 292 additions & 230 deletions

File tree

buildSrc/src/main/java/org/springframework/boot/build/bom/CheckLinks.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@
2828
import org.gradle.api.tasks.TaskAction;
2929
import org.gradle.internal.impldep.org.apache.http.client.config.CookieSpecs;
3030

31-
import org.springframework.http.HttpMethod;
3231
import org.springframework.http.ResponseEntity;
3332
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
34-
import org.springframework.web.client.NoOpResponseErrorHandler;
35-
import org.springframework.web.client.RestTemplate;
33+
import org.springframework.web.client.RestClient;
34+
import org.springframework.web.client.RestClient.ResponseSpec.ErrorHandler;
3635

3736
/**
3837
* Task to check that links are working.
@@ -42,6 +41,9 @@
4241
*/
4342
public abstract class CheckLinks extends DefaultTask {
4443

44+
private static final ErrorHandler NOOP_ERROR_HANDLER = (request, response) -> {
45+
};
46+
4547
private final BomExtension bom;
4648

4749
@Inject
@@ -54,14 +56,16 @@ void releaseNotes() {
5456
RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
5557
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(config).build();
5658
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
57-
RestTemplate restTemplate = new RestTemplate(requestFactory);
58-
restTemplate.setErrorHandler(new NoOpResponseErrorHandler());
59+
RestClient restClient = RestClient.builder()
60+
.requestFactory(requestFactory)
61+
.defaultStatusHandler((status) -> true, NOOP_ERROR_HANDLER)
62+
.build();
5963
for (Library library : this.bom.getLibraries()) {
6064
library.getLinks().forEach((name, links) -> links.forEach((link) -> {
6165
URI uri;
6266
try {
6367
uri = new URI(link.url(library));
64-
ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.HEAD, null, String.class);
68+
ResponseEntity<String> response = restClient.head().uri(uri).retrieve().toEntity(String.class);
6569
System.out.printf("[%3d] %s - %s (%s)%n", response.getStatusCode().value(), library.getName(), name,
6670
uri);
6771
}

buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/MavenMetadataVersionResolver.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.net.URI;
2020
import java.util.Collection;
21-
import java.util.Collections;
2221
import java.util.HashSet;
2322
import java.util.Set;
2423
import java.util.SortedSet;
@@ -37,13 +36,10 @@
3736

3837
import org.springframework.boot.build.bom.bomr.version.DependencyVersion;
3938
import org.springframework.boot.build.xml.XmlDocument;
40-
import org.springframework.http.HttpEntity;
41-
import org.springframework.http.HttpHeaders;
42-
import org.springframework.http.HttpMethod;
4339
import org.springframework.http.HttpStatus;
4440
import org.springframework.http.converter.StringHttpMessageConverter;
4541
import org.springframework.web.client.HttpClientErrorException;
46-
import org.springframework.web.client.RestTemplate;
42+
import org.springframework.web.client.RestClient;
4743
import org.springframework.web.util.UriComponentsBuilder;
4844

4945
/**
@@ -54,16 +50,19 @@
5450
*/
5551
final class MavenMetadataVersionResolver implements VersionResolver {
5652

57-
private final RestTemplate rest;
53+
private final RestClient rest;
5854

5955
private final Collection<MavenArtifactRepository> repositories;
6056

6157
MavenMetadataVersionResolver(Collection<MavenArtifactRepository> repositories) {
62-
this(new RestTemplate(Collections.singletonList(new StringHttpMessageConverter())), repositories);
58+
this(RestClient.builder()
59+
.configureMessageConverters(
60+
(converters) -> converters.disableDefaults().withStringConverter(new StringHttpMessageConverter()))
61+
.build(), repositories);
6362
}
6463

65-
MavenMetadataVersionResolver(RestTemplate restTemplate, Collection<MavenArtifactRepository> repositories) {
66-
this.rest = restTemplate;
64+
MavenMetadataVersionResolver(RestClient restClient, Collection<MavenArtifactRepository> repositories) {
65+
this.rest = restClient;
6766
this.repositories = repositories;
6867
}
6968

@@ -83,14 +82,13 @@ private Set<String> resolveVersions(String groupId, String artifactId, MavenArti
8382
.build()
8483
.toUri();
8584
try {
86-
HttpHeaders headers = new HttpHeaders();
87-
PasswordCredentials credentials = credentialsOf(repository);
88-
String username = (credentials != null) ? credentials.getUsername() : null;
89-
if (username != null) {
90-
headers.setBasicAuth(username, credentials.getPassword());
91-
}
92-
HttpEntity<Void> request = new HttpEntity<>(headers);
93-
String metadata = this.rest.exchange(url, HttpMethod.GET, request, String.class).getBody();
85+
String metadata = this.rest.get().uri(url).headers((headers) -> {
86+
PasswordCredentials credentials = credentialsOf(repository);
87+
String username = (credentials != null) ? credentials.getUsername() : null;
88+
if (username != null) {
89+
headers.setBasicAuth(username, credentials.getPassword());
90+
}
91+
}).retrieve().body(String.class);
9492
Document metadataDocument = XmlDocument.parseContent(metadata);
9593
NodeList versionNodes = (NodeList) XPathFactory.newInstance()
9694
.newXPath()

buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/ReleaseSchedule.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
import org.springframework.boot.build.bom.bomr.version.DependencyVersion;
2929
import org.springframework.http.ResponseEntity;
3030
import org.springframework.util.LinkedCaseInsensitiveMap;
31-
import org.springframework.web.client.RestOperations;
32-
import org.springframework.web.client.RestTemplate;
31+
import org.springframework.web.client.RestClient;
3332

3433
/**
3534
* Release schedule for Spring projects, retrieved from
@@ -41,20 +40,22 @@ class ReleaseSchedule {
4140

4241
private static final Pattern LIBRARY_AND_VERSION = Pattern.compile("([A-Za-z0-9 ]+) ([0-9A-Za-z.-]+)");
4342

44-
private final RestOperations rest;
43+
private final RestClient rest;
4544

4645
ReleaseSchedule() {
47-
this(new RestTemplate());
46+
this(RestClient.create());
4847
}
4948

50-
ReleaseSchedule(RestOperations rest) {
49+
ReleaseSchedule(RestClient rest) {
5150
this.rest = rest;
5251
}
5352

5453
@SuppressWarnings({ "unchecked", "rawtypes" })
5554
Map<String, List<Release>> releasesBetween(OffsetDateTime start, OffsetDateTime end) {
56-
ResponseEntity<List> response = this.rest
57-
.getForEntity("https://calendar.spring.io/releases?start=" + start + "&end=" + end, List.class);
55+
ResponseEntity<List> response = this.rest.get()
56+
.uri("https://calendar.spring.io/releases?start=" + start + "&end=" + end)
57+
.retrieve()
58+
.toEntity(List.class);
5859
List<Map<String, String>> body = response.getBody();
5960
Map<String, List<Release>> releasesByLibrary = new LinkedCaseInsensitiveMap<>();
6061
body.stream()

buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/github/Issue.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import java.util.List;
2121
import java.util.Map;
2222

23-
import org.springframework.web.client.RestTemplate;
23+
import org.springframework.web.client.RestClient;
2424

2525
/**
2626
* Minimal representation of a GitHub issue.
@@ -29,15 +29,15 @@
2929
*/
3030
public class Issue {
3131

32-
private final RestTemplate rest;
32+
private final RestClient rest;
3333

3434
private final int number;
3535

3636
private final String title;
3737

3838
private final State state;
3939

40-
Issue(RestTemplate rest, int number, String title, State state) {
40+
Issue(RestClient rest, int number, String title, State state) {
4141
this.rest = rest;
4242
this.number = number;
4343
this.title = title;
@@ -62,7 +62,7 @@ public State getState() {
6262
*/
6363
public void label(List<String> labels) {
6464
Map<String, List<String>> body = Collections.singletonMap("labels", labels);
65-
this.rest.put("issues/" + this.number + "/labels", body);
65+
this.rest.put().uri("issues/" + this.number + "/labels").body(body).retrieve().toBodilessEntity();
6666
}
6767

6868
public enum State {

buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/github/StandardGitHub.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@
1717
package org.springframework.boot.build.bom.bomr.github;
1818

1919
import java.util.Base64;
20-
import java.util.Collections;
2120

2221
import org.springframework.http.MediaType;
2322
import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter;
24-
import org.springframework.web.client.RestTemplate;
25-
import org.springframework.web.util.DefaultUriBuilderFactory;
26-
import org.springframework.web.util.UriTemplateHandler;
23+
import org.springframework.web.client.RestClient;
2724

2825
/**
2926
* Standard implementation of {@link GitHub}.
@@ -43,24 +40,24 @@ final class StandardGitHub implements GitHub {
4340

4441
@Override
4542
public GitHubRepository getRepository(String organization, String name) {
46-
RestTemplate restTemplate = createRestTemplate();
47-
restTemplate.getInterceptors().add((request, body, execution) -> {
48-
request.getHeaders().add("User-Agent", StandardGitHub.this.username);
49-
request.getHeaders()
50-
.add("Authorization", "Basic " + Base64.getEncoder()
51-
.encodeToString((StandardGitHub.this.username + ":" + StandardGitHub.this.password).getBytes()));
52-
request.getHeaders().add("Accept", MediaType.APPLICATION_JSON_VALUE);
53-
return execution.execute(request, body);
54-
});
55-
UriTemplateHandler uriTemplateHandler = new DefaultUriBuilderFactory(
56-
"https://api.github.com/repos/" + organization + "/" + name + "/");
57-
restTemplate.setUriTemplateHandler(uriTemplateHandler);
58-
return new StandardGitHubRepository(restTemplate);
43+
return new StandardGitHubRepository(createRestClient(organization, name));
5944
}
6045

61-
@SuppressWarnings({ "deprecation", "removal" })
62-
private RestTemplate createRestTemplate() {
63-
return new RestTemplate(Collections.singletonList(new JacksonJsonHttpMessageConverter()));
46+
private RestClient createRestClient(String organization, String name) {
47+
return RestClient.builder()
48+
.baseUrl("https://api.github.com/repos/" + organization + "/" + name + "/")
49+
.configureMessageConverters((converters) -> converters.disableDefaults()
50+
.withJsonConverter(new JacksonJsonHttpMessageConverter()))
51+
.requestInterceptor((request, body, execution) -> {
52+
request.getHeaders().add("User-Agent", StandardGitHub.this.username);
53+
request.getHeaders()
54+
.add("Authorization", "Basic " + Base64.getEncoder()
55+
.encodeToString(
56+
(StandardGitHub.this.username + ":" + StandardGitHub.this.password).getBytes()));
57+
request.getHeaders().add("Accept", MediaType.APPLICATION_JSON_VALUE);
58+
return execution.execute(request, body);
59+
})
60+
.build();
6461
}
6562

6663
}

buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/github/StandardGitHubRepository.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727

2828
import org.springframework.http.ResponseEntity;
2929
import org.springframework.web.client.HttpClientErrorException.Forbidden;
30+
import org.springframework.web.client.RestClient;
3031
import org.springframework.web.client.RestClientException;
31-
import org.springframework.web.client.RestTemplate;
3232

3333
/**
3434
* Standard implementation of {@link GitHubRepository}.
@@ -37,10 +37,10 @@
3737
*/
3838
final class StandardGitHubRepository implements GitHubRepository {
3939

40-
private final RestTemplate rest;
40+
private final RestClient rest;
4141

42-
StandardGitHubRepository(RestTemplate restTemplate) {
43-
this.rest = restTemplate;
42+
StandardGitHubRepository(RestClient restClient) {
43+
this.rest = restClient;
4444
}
4545

4646
@Override
@@ -56,7 +56,11 @@ public int openIssue(String title, String body, List<String> labels, Milestone m
5656
}
5757
requestBody.put("body", body);
5858
try {
59-
ResponseEntity<Map> response = this.rest.postForEntity("issues", requestBody, Map.class);
59+
ResponseEntity<Map> response = this.rest.post()
60+
.uri("issues")
61+
.body(requestBody)
62+
.retrieve()
63+
.toEntity(Map.class);
6064
// See gh-30304
6165
sleep(Duration.ofSeconds(3));
6266
return (Integer) response.getBody().get("number");
@@ -92,7 +96,7 @@ public List<Issue> findIssues(List<String> labels, Milestone milestone) {
9296

9397
@SuppressWarnings({ "rawtypes", "unchecked" })
9498
private <T> List<T> get(String name, Function<Map<String, Object>, T> mapper) {
95-
ResponseEntity<List> response = this.rest.getForEntity(name, List.class);
99+
ResponseEntity<List> response = this.rest.get().uri(name).retrieve().toEntity(List.class);
96100
return ((List<Map<String, Object>>) response.getBody()).stream().map(mapper).toList();
97101
}
98102

buildSrc/src/test/java/org/springframework/boot/build/bom/bomr/ReleaseScheduleTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.springframework.core.io.ClassPathResource;
2727
import org.springframework.http.MediaType;
2828
import org.springframework.test.web.client.MockRestServiceServer;
29-
import org.springframework.web.client.RestTemplate;
29+
import org.springframework.web.client.RestClient;
3030

3131
import static org.assertj.core.api.Assertions.assertThat;
3232
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
@@ -39,11 +39,11 @@
3939
*/
4040
class ReleaseScheduleTests {
4141

42-
private final RestTemplate rest = new RestTemplate();
42+
private final RestClient.Builder restBuilder = RestClient.builder();
4343

44-
private final ReleaseSchedule releaseSchedule = new ReleaseSchedule(this.rest);
44+
private final MockRestServiceServer server = MockRestServiceServer.bindTo(this.restBuilder).build();
4545

46-
private final MockRestServiceServer server = MockRestServiceServer.bindTo(this.rest).build();
46+
private final ReleaseSchedule releaseSchedule = new ReleaseSchedule(this.restBuilder.build());
4747

4848
@Test
4949
void releasesBetween() {

integration-test/spring-boot-integration-tests/src/test/java/org/springframework/boot/web/servlet/support/ErrorPageFilterIntegrationTests.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import org.springframework.web.bind.annotation.RequestMapping;
4646
import org.springframework.web.bind.annotation.ResponseBody;
4747
import org.springframework.web.bind.annotation.ResponseStatus;
48-
import org.springframework.web.client.RestTemplate;
48+
import org.springframework.web.client.RestClient;
4949
import org.springframework.web.servlet.DispatcherServlet;
5050
import org.springframework.web.servlet.HandlerInterceptor;
5151
import org.springframework.web.servlet.ModelAndView;
@@ -93,9 +93,11 @@ void ok() throws Exception {
9393
private void doTest(AnnotationConfigServletWebServerApplicationContext context, String resourcePath,
9494
HttpStatus status) throws Exception {
9595
int port = context.getWebServer().getPort();
96-
RestTemplate template = new RestTemplate();
97-
ResponseEntity<String> entity = template.getForEntity(new URI("http://localhost:" + port + resourcePath),
98-
String.class);
96+
RestClient restClient = RestClient.create();
97+
ResponseEntity<String> entity = restClient.get()
98+
.uri(new URI("http://localhost:" + port + resourcePath))
99+
.retrieve()
100+
.toEntity(String.class);
99101
assertThat(entity.getBody()).isEqualTo("Hello World");
100102
assertThat(entity.getStatusCode()).isEqualTo(status);
101103
}

0 commit comments

Comments
 (0)