Upgrade to Spring Boot 2.3 and related changes.

This commit is contained in:
Greg L. Turnquist
2020-10-20 16:24:26 -05:00
parent 1d12ed185c
commit 5f74daf4bd
13 changed files with 44 additions and 35 deletions

View File

@@ -189,8 +189,8 @@ public class HomeController {
private final RestTemplate rest;
public HomeController(RestTemplate restTemplate) {
this.rest = restTemplate;
public HomeController(RestTemplateBuilder restTemplateBuilder) {
this.rest = restTemplateBuilder.build();
}
...
}
@@ -202,7 +202,8 @@ service. So in this example, it is hard coded into place.
WARNING: For fault tolerant production systems, hard coded URIs are NOT recommended. Instead, use something like
Spring Cloud Netflix and it's Eureka/Ribbon features to allow https://spring.io/guides/gs/service-registration-and-discovery/[service discovery] and https://spring.io/guides/gs/client-side-load-balancing/[load balanced calls].
Parts of the controller must also perform REST calls, so we request a `RestTemplate` in the constructor call, allowing Spring to provide it.
Parts of the controller must also perform REST calls, so we request a `RestTemplateBuilder` in the constructor call, allowing Spring Boot to provide it.
Having been decorated with the `HypermediaRestTemplateConfigurer`, it has all active media types applied. You are free to further customize things before invoking the `build()` operation that yields a `RestTemplate`.
To construct a listing of all employees, check out the following controller method:

View File

@@ -15,9 +15,10 @@
*/
package org.springframework.hateoas.examples;
import org.springframework.boot.web.client.RestTemplateCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.hateoas.config.HypermediaRestTemplateConfigurer;
/**
* @author Greg Turnquist
@@ -26,7 +27,9 @@ import org.springframework.web.client.RestTemplate;
public class ClientConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
RestTemplateCustomizer hypermediaRestTemplateCustomizer(HypermediaRestTemplateConfigurer configurer) {
return restTemplate -> {
configurer.registerHypermediaTypes(restTemplate);
};
}
}

View File

@@ -18,6 +18,7 @@ package org.springframework.hateoas.examples;
import java.net.URI;
import java.net.URISyntaxException;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.Link;
@@ -41,10 +42,10 @@ public class HomeController {
private static final String REMOTE_SERVICE_ROOT_URI = "http://localhost:9000";
private final RestTemplate rest;
private RestTemplate rest;
public HomeController(RestTemplate restTemplate) {
this.rest = restTemplate;
public HomeController(RestTemplateBuilder restTemplateBuilder) {
this.rest = restTemplateBuilder.build();
}
/**

View File

@@ -15,9 +15,10 @@
*/
package org.springframework.hateoas.examples;
import org.springframework.boot.web.client.RestTemplateCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.hateoas.config.HypermediaRestTemplateConfigurer;
/**
* @author Greg Turnquist
@@ -26,7 +27,9 @@ import org.springframework.web.client.RestTemplate;
public class ClientConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
RestTemplateCustomizer hypermediaRestTemplateCustomizer(HypermediaRestTemplateConfigurer configurer) {
return restTemplate -> {
configurer.registerHypermediaTypes(restTemplate);
};
}
}

View File

@@ -18,6 +18,7 @@ package org.springframework.hateoas.examples;
import java.net.URI;
import java.net.URISyntaxException;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.Link;
@@ -43,8 +44,8 @@ public class HomeController {
private final RestTemplate rest;
public HomeController(RestTemplate restTemplate) {
this.rest = restTemplate;
public HomeController(RestTemplateBuilder restTemplateBuilder) {
this.rest = restTemplateBuilder.build();
}
/**