Update spring-boot-sample-test REST code

Update RemoteVehicleDetailsService and the related test to use the new
RestTemplateBuilder class and @RestClientTest annotation.

See gh-6030
See gh-5507
This commit is contained in:
Phillip Webb
2016-05-31 09:29:24 -07:00
parent 0a475946a1
commit b84330663d
3 changed files with 20 additions and 25 deletions

View File

@@ -20,6 +20,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sample.test.domain.VehicleIdentificationNumber;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
@@ -37,27 +38,22 @@ public class RemoteVehicleDetailsService implements VehicleDetailsService {
private static final Logger logger = LoggerFactory
.getLogger(RemoteVehicleDetailsService.class);
private final ServiceProperties properties;
private final RestTemplate restTemplate;
public RemoteVehicleDetailsService(ServiceProperties properties) {
this.properties = properties;
this.restTemplate = new RestTemplate();
}
protected final RestTemplate getRestTemplate() {
return this.restTemplate;
public RemoteVehicleDetailsService(ServiceProperties properties,
RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder
.rootUri(properties.getVehicleServiceRootUrl()).build();
}
@Override
public VehicleDetails getVehicleDetails(VehicleIdentificationNumber vin)
throws VehicleIdentificationNumberNotFoundException {
Assert.notNull(vin, "VIN must not be null");
String url = this.properties.getVehicleServiceRootUrl() + "vehicle/{vin}/details";
logger.debug("Retrieving vehicle data for: " + vin + " from: " + url);
logger.debug("Retrieving vehicle data for: " + vin);
try {
return this.restTemplate.getForObject(url, VehicleDetails.class, vin);
return this.restTemplate.getForObject("/vehicle/{vin}/details",
VehicleDetails.class, vin);
}
catch (HttpStatusCodeException ex) {
if (HttpStatus.NOT_FOUND.equals(ex.getStatusCode())) {

View File

@@ -28,7 +28,7 @@ import org.springframework.stereotype.Component;
@ConfigurationProperties
public class ServiceProperties {
private String vehicleServiceRootUrl = "http://localhost:8080/vs/";
private String vehicleServiceRootUrl = "http://localhost:8080/vs";
public String getVehicleServiceRootUrl() {
return this.vehicleServiceRootUrl;