diff --git a/samples/standalone/restdocs/http-client/src/main/java/com/example/loan/Application.java b/samples/standalone/restdocs/http-client/src/main/java/com/example/loan/Application.java index 8c680cb15e..e8b5016451 100644 --- a/samples/standalone/restdocs/http-client/src/main/java/com/example/loan/Application.java +++ b/samples/standalone/restdocs/http-client/src/main/java/com/example/loan/Application.java @@ -18,8 +18,10 @@ package com.example.loan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; @SpringBootApplication +@EnableConfigurationProperties(ServiceConfiguration.class) public class Application { public static void main(String[] args) { diff --git a/samples/standalone/restdocs/http-client/src/main/java/com/example/loan/LoanApplicationService.java b/samples/standalone/restdocs/http-client/src/main/java/com/example/loan/LoanApplicationService.java index ce6b386a28..f70bc98fed 100644 --- a/samples/standalone/restdocs/http-client/src/main/java/com/example/loan/LoanApplicationService.java +++ b/samples/standalone/restdocs/http-client/src/main/java/com/example/loan/LoanApplicationService.java @@ -33,20 +33,20 @@ import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service -@ConfigurationProperties("service") public class LoanApplicationService { private static final String FRAUD_SERVICE_JSON_VERSION_1 = "application/vnd.fraud.v1+json"; private final RestTemplate restTemplate; - private int port = 8080; + private final ServiceConfiguration serviceConfiguration; - public LoanApplicationService() { + public LoanApplicationService(ServiceConfiguration serviceConfiguration) { this.restTemplate = new RestTemplate(); // tag::custom_request_factory[] this.restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory()); // end::custom_request_factory[] + this.serviceConfiguration = serviceConfiguration; } public LoanApplicationResult loanApplication(LoanApplication loanApplication) { @@ -64,7 +64,7 @@ public class LoanApplicationService { // tag::client_call_server[] ResponseEntity response = restTemplate.exchange( - "http://localhost:" + port + "/fraudcheck", HttpMethod.PUT, + "http://localhost:" + getPort() + "/fraudcheck", HttpMethod.PUT, new HttpEntity<>(request, httpHeaders), FraudServiceResponse.class); // end::client_call_server[] @@ -85,8 +85,26 @@ public class LoanApplicationService { response.getRejectionReason()); } + public int getPort() { + return this.serviceConfiguration.getPort(); + } + public void setPort(int port) { - this.port = port; + this.serviceConfiguration.setPort(port); } } + +@ConfigurationProperties("service") +class ServiceConfiguration { + + private int port = 8080; + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } +} diff --git a/samples/standalone/restdocs/http-client/src/main/java/com/example/loan/ServiceProperties.java b/samples/standalone/restdocs/http-client/src/main/java/com/example/loan/ServiceProperties.java deleted file mode 100644 index 732dc54282..0000000000 --- a/samples/standalone/restdocs/http-client/src/main/java/com/example/loan/ServiceProperties.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2013-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. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.loan; - -import org.springframework.boot.context.properties.ConfigurationProperties; - -@ConfigurationProperties("service") -public class ServiceProperties { - - private int port = 8080; - - public int getPort() { - return this.port; - } - - public void setPort(int port) { - this.port = port; - } - -} diff --git a/samples/standalone/webclient/http-client/src/main/java/com/example/loan/Application.java b/samples/standalone/webclient/http-client/src/main/java/com/example/loan/Application.java index 8c680cb15e..e8b5016451 100644 --- a/samples/standalone/webclient/http-client/src/main/java/com/example/loan/Application.java +++ b/samples/standalone/webclient/http-client/src/main/java/com/example/loan/Application.java @@ -18,8 +18,10 @@ package com.example.loan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; @SpringBootApplication +@EnableConfigurationProperties(ServiceConfiguration.class) public class Application { public static void main(String[] args) { diff --git a/samples/standalone/webclient/http-client/src/main/java/com/example/loan/LoanApplicationService.java b/samples/standalone/webclient/http-client/src/main/java/com/example/loan/LoanApplicationService.java index 3c52416dab..8b547278c9 100644 --- a/samples/standalone/webclient/http-client/src/main/java/com/example/loan/LoanApplicationService.java +++ b/samples/standalone/webclient/http-client/src/main/java/com/example/loan/LoanApplicationService.java @@ -32,17 +32,17 @@ import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service -@ConfigurationProperties("service") public class LoanApplicationService { private static final String FRAUD_SERVICE_JSON_VERSION_1 = "application/vnd.fraud.v1+json"; private final RestTemplate restTemplate; - private int port = 8080; + private final ServiceConfiguration serviceConfiguration; - public LoanApplicationService() { + public LoanApplicationService(ServiceConfiguration serviceConfiguration) { this.restTemplate = new RestTemplate(); + this.serviceConfiguration = serviceConfiguration; } public LoanApplicationResult loanApplication(LoanApplication loanApplication) { @@ -60,7 +60,7 @@ public class LoanApplicationService { // tag::client_call_server[] ResponseEntity response = this.restTemplate.exchange( - "http://localhost:" + this.port + "/fraudcheck", HttpMethod.PUT, + "http://localhost:" + getPort() + "/fraudcheck", HttpMethod.PUT, new HttpEntity<>(request, httpHeaders), FraudServiceResponse.class); // end::client_call_server[] @@ -81,8 +81,26 @@ public class LoanApplicationService { response.getRejectionReason()); } + public int getPort() { + return this.serviceConfiguration.getPort(); + } + public void setPort(int port) { - this.port = port; + this.serviceConfiguration.setPort(port); } } + +@ConfigurationProperties("service") +class ServiceConfiguration { + + private int port = 8080; + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } +} \ No newline at end of file