Skip to content

Commit 47528a1

Browse files
committed
Upgrade to Spring Boot 4.0.x and Java 25
1 parent c80ec17 commit 47528a1

File tree

21 files changed

+387
-593
lines changed

21 files changed

+387
-593
lines changed

03.microservices/01-step-by-step-changes/readme.md

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@
4343
<parent>
4444
<groupId>org.springframework.boot</groupId>
4545
<artifactId>spring-boot-starter-parent</artifactId>
46-
<version>2.7.3</version>
46+
<version>4.0.0</version>
4747
<relativePath/> <!-- lookup parent from repository -->
4848
</parent>
4949

5050
<properties>
51-
<java.version>21</java.version>
52-
<spring-cloud.version>2021.0.3</spring-cloud.version>
51+
<java.version>25</java.version>
52+
<spring-cloud.version>2025.1.0-RC1</spring-cloud.version>
5353
</properties>
5454
```
5555

@@ -1040,6 +1040,24 @@ public class CurrencyConversion {
10401040

10411041
Step 17 - Invoking Currency Exchange Microservice from Currency Conversion Microservice
10421042

1043+
> Starting with **Spring Boot 4**, the recommended way to call external APIs is to use **RestClient**, as **RestTemplate** is planned for deprecation.
1044+
1045+
To use **RestClient**, you need to add the **HTTP Client** starter in `start.spring.io`.
1046+
1047+
Follow these steps:
1048+
1049+
1. Open **start.spring.io**
1050+
2. Click **Add Dependencies**
1051+
3. Type `client` in the search box
1052+
4. Select **HTTP Client** from the list
1053+
1054+
```xml
1055+
<dependency>
1056+
<groupId>org.springframework.boot</groupId>
1057+
<artifactId>spring-boot-starter-restclient</artifactId>
1058+
</dependency>
1059+
```
1060+
10431061
#### /currency-conversion-service/src/main/java/com/in28minutes/microservices/currencyconversionservice/CurrencyConversionController.java Modified
10441062

10451063
```java
@@ -1052,11 +1070,22 @@ import org.springframework.http.ResponseEntity;
10521070
import org.springframework.web.bind.annotation.GetMapping;
10531071
import org.springframework.web.bind.annotation.PathVariable;
10541072
import org.springframework.web.bind.annotation.RestController;
1055-
import org.springframework.web.client.RestTemplate;
1073+
// import org.springframework.web.client.RestTemplate;
1074+
1075+
@Configuration(proxyBeanMethods = false)
1076+
class RestClientConfiguration {
1077+
1078+
@Bean
1079+
RestClient restClient(RestClient.Builder builder) {
1080+
return builder.build();
1081+
}
1082+
}
10561083

10571084
@RestController
10581085
public class CurrencyConversionController {
1059-
1086+
1087+
@Autowired
1088+
private RestClient restClient;
10601089

10611090
@GetMapping("/currency-conversion/from/{from}/to/{to}/quantity/{quantity}")
10621091
public CurrencyConversion calculateCurrencyConversion(
@@ -1068,18 +1097,29 @@ public class CurrencyConversionController {
10681097
HashMap<String, String> uriVariables = new HashMap<>();
10691098
uriVariables.put("from",from);
10701099
uriVariables.put("to",to);
1100+
1101+
CurrencyConversion currencyConversion = restClient.get()
1102+
.uri("http://localhost:8000/currency-exchange/from/{from}/to/{to}", uriVariables)
1103+
.retrieve()
1104+
.body(CurrencyConversion.class);
1105+
1106+
return new CurrencyConversion(currencyConversion.getId(),
1107+
from, to, quantity,
1108+
currencyConversion.getConversionMultiple(),
1109+
quantity.multiply(currencyConversion.getConversionMultiple()),
1110+
currencyConversion.getEnvironment()+ " " + "rest client");
10711111

1072-
ResponseEntity<CurrencyConversion> responseEntity = new RestTemplate().getForEntity
1073-
("http://localhost:8000/currency-exchange/from/{from}/to/{to}",
1074-
CurrencyConversion.class, uriVariables);
1075-
1076-
CurrencyConversion currencyConversion = responseEntity.getBody();
1077-
1078-
return new CurrencyConversion(currencyConversion.getId(),
1079-
from, to, quantity,
1080-
currencyConversion.getConversionMultiple(),
1081-
quantity.multiply(currencyConversion.getConversionMultiple()),
1082-
currencyConversion.getEnvironment()+ " " + "rest template");
1112+
// ResponseEntity<CurrencyConversion> responseEntity = new RestTemplate().getForEntity
1113+
// ("http://localhost:8000/currency-exchange/from/{from}/to/{to}",
1114+
// CurrencyConversion.class, uriVariables);
1115+
//
1116+
// CurrencyConversion currencyConversion = responseEntity.getBody();
1117+
//
1118+
// return new CurrencyConversion(currencyConversion.getId(),
1119+
// from, to, quantity,
1120+
// currencyConversion.getConversionMultiple(),
1121+
// quantity.multiply(currencyConversion.getConversionMultiple()),
1122+
// currencyConversion.getEnvironment()+ " " + "rest template");
10831123

10841124
}
10851125

@@ -1684,10 +1724,15 @@ Changes for:
16841724

16851725
#### /currency-exchange-service/pom.xml Modified
16861726
New Lines
1727+
16871728
```xml
1729+
<properties>
1730+
<spring-aop.version>4.0.0-M2</spring-aop.version>
1731+
</properties>
16881732
<dependency>
16891733
<groupId>org.springframework.boot</groupId>
16901734
<artifactId>spring-boot-starter-aop</artifactId>
1735+
<version>${spring-aop.version}</version>
16911736
</dependency>
16921737

16931738
<dependency>

03.microservices/api-gateway/pom.xml

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.springframework.boot</groupId>
77
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>3.5.0</version>
8+
<version>4.0.0</version>
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
1111
<groupId>com.in28minutes.microservices</groupId>
@@ -15,8 +15,8 @@
1515
<description>Demo project for Spring Boot</description>
1616

1717
<properties>
18-
<java.version>21</java.version>
19-
<spring-cloud.version>2025.0.0</spring-cloud.version>
18+
<java.version>25</java.version>
19+
<spring-cloud.version>2025.1.0-RC1</spring-cloud.version>
2020
</properties>
2121

2222
<dependencies>
@@ -98,16 +98,20 @@
9898
<artifactId>opentelemetry-exporter-zipkin</artifactId>
9999
</dependency>
100100

101-
102101
<dependency>
103102
<groupId>org.springframework.boot</groupId>
104103
<artifactId>spring-boot-devtools</artifactId>
105104
<scope>runtime</scope>
106105
<optional>true</optional>
107106
</dependency>
107+
<dependency>
108+
<groupId>org.springframework.boot</groupId>
109+
<artifactId>spring-boot-starter-actuator-test</artifactId>
110+
<scope>test</scope>
111+
</dependency>
108112
<dependency>
109113
<groupId>org.springframework.boot</groupId>
110-
<artifactId>spring-boot-starter-test</artifactId>
114+
<artifactId>spring-boot-starter-webmvc-test</artifactId>
111115
<scope>test</scope>
112116
</dependency>
113117
</dependencies>
@@ -133,12 +137,4 @@
133137
</plugins>
134138
</build>
135139

136-
<repositories>
137-
<repository>
138-
<id>spring-milestones</id>
139-
<name>Spring Milestones</name>
140-
<url>https://repo.spring.io/milestone</url>
141-
</repository>
142-
</repositories>
143-
144140
</project>

03.microservices/currency-conversion-service/pom.xml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.springframework.boot</groupId>
77
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>3.5.0</version>
8+
<version>4.0.0</version>
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
1111
<groupId>com.in28minutes.microservices</groupId>
@@ -15,8 +15,8 @@
1515
<description>Demo project for Spring Boot</description>
1616

1717
<properties>
18-
<java.version>21</java.version>
19-
<spring-cloud.version>2025.0.0</spring-cloud.version>
18+
<java.version>25</java.version>
19+
<spring-cloud.version>2025.1.0-RC1</spring-cloud.version>
2020
</properties>
2121

2222
<dependencies>
@@ -26,8 +26,13 @@
2626
</dependency>
2727
<dependency>
2828
<groupId>org.springframework.boot</groupId>
29-
<artifactId>spring-boot-starter-web</artifactId>
29+
<artifactId>spring-boot-starter-webmvc</artifactId>
3030
</dependency>
31+
<!-- RestTemplate Deprecated, please use restclient with the spring boot starter -->
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-restclient</artifactId>
35+
</dependency>
3136
<dependency>
3237
<groupId>org.springframework.cloud</groupId>
3338
<artifactId>spring-cloud-starter-config</artifactId>
@@ -112,9 +117,14 @@
112117
<scope>runtime</scope>
113118
<optional>true</optional>
114119
</dependency>
120+
<dependency>
121+
<groupId>org.springframework.boot</groupId>
122+
<artifactId>spring-boot-starter-actuator-test</artifactId>
123+
<scope>test</scope>
124+
</dependency>
115125
<dependency>
116126
<groupId>org.springframework.boot</groupId>
117-
<artifactId>spring-boot-starter-test</artifactId>
127+
<artifactId>spring-boot-starter-webmvc-test</artifactId>
118128
<scope>test</scope>
119129
</dependency>
120130
</dependencies>
@@ -139,13 +149,4 @@
139149
</plugin>
140150
</plugins>
141151
</build>
142-
143-
<repositories>
144-
<repository>
145-
<id>spring-milestones</id>
146-
<name>Spring Milestones</name>
147-
<url>https://repo.spring.io/milestone</url>
148-
</repository>
149-
</repositories>
150-
151152
</project>

03.microservices/currency-conversion-service/src/main/java/com/in28minutes/microservices/currencyconversionservice/CurrencyConversionController.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,31 @@
44
import java.util.HashMap;
55

66
import org.springframework.beans.factory.annotation.Autowired;
7-
import org.springframework.boot.web.client.RestTemplateBuilder;
7+
//import org.springframework.boot.web.client.RestTemplateBuilder;
88
import org.springframework.context.annotation.Bean;
99
import org.springframework.context.annotation.Configuration;
1010
import org.springframework.http.ResponseEntity;
1111
import org.springframework.web.bind.annotation.GetMapping;
1212
import org.springframework.web.bind.annotation.PathVariable;
1313
import org.springframework.web.bind.annotation.RestController;
14+
import org.springframework.web.client.RestClient;
1415
import org.springframework.web.client.RestTemplate;
1516

17+
// Enable for Spring Boot 3.0.x
18+
//@Configuration(proxyBeanMethods = false)
19+
//class RestTemplateConfiguration {
20+
//
21+
// @Bean
22+
// RestTemplate restTemplate(RestTemplateBuilder builder) {
23+
// return builder.build();
24+
// }
25+
//}
26+
1627
@Configuration(proxyBeanMethods = false)
17-
class RestTemplateConfiguration {
18-
28+
class RestClientConfiguration {
29+
1930
@Bean
20-
RestTemplate restTemplate(RestTemplateBuilder builder) {
31+
RestClient restClient(RestClient.Builder builder) {
2132
return builder.build();
2233
}
2334
}
@@ -27,9 +38,13 @@ public class CurrencyConversionController {
2738

2839
@Autowired
2940
private CurrencyExchangeProxy proxy;
30-
31-
@Autowired
32-
private RestTemplate restTemplate;
41+
42+
// Uncomment for Spring Boot 3.0.x
43+
// @Autowired
44+
// private RestTemplate restTemplate;
45+
46+
@Autowired
47+
private RestClient restClient;
3348

3449

3550
@GetMapping("/currency-conversion/from/{from}/to/{to}/quantity/{quantity}")
@@ -43,17 +58,22 @@ public CurrencyConversion calculateCurrencyConversion(
4358
uriVariables.put("from",from);
4459
uriVariables.put("to",to);
4560

46-
ResponseEntity<CurrencyConversion> responseEntity = restTemplate.getForEntity
47-
("http://localhost:8000/currency-exchange/from/{from}/to/{to}",
48-
CurrencyConversion.class, uriVariables);
61+
// ResponseEntity<CurrencyConversion> responseEntity = restTemplate.getForEntity
62+
// ("http://localhost:8000/currency-exchange/from/{from}/to/{to}",
63+
// CurrencyConversion.class, uriVariables);
64+
65+
CurrencyConversion currencyConversion = restClient.get()
66+
.uri("http://localhost:8000/currency-exchange/from/{from}/to/{to}", uriVariables)
67+
.retrieve()
68+
.body(CurrencyConversion.class);
4969

50-
CurrencyConversion currencyConversion = responseEntity.getBody();
70+
// CurrencyConversion currencyConversion = responseEntity.getBody();
5171

5272
return new CurrencyConversion(currencyConversion.getId(),
5373
from, to, quantity,
5474
currencyConversion.getConversionMultiple(),
5575
quantity.multiply(currencyConversion.getConversionMultiple()),
56-
currencyConversion.getEnvironment()+ " " + "rest template");
76+
currencyConversion.getEnvironment()+ " " + "rest client");
5777

5878
}
5979

03.microservices/currency-exchange-service/pom.xml

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.springframework.boot</groupId>
77
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>3.5.0</version>
8+
<version>4.0.0</version>
99
<relativePath/> <!-- lookup parent from repository -->
1010
</parent>
1111
<groupId>com.in28minutes.microservices</groupId>
@@ -15,8 +15,9 @@
1515
<description>Demo project for Spring Boot</description>
1616

1717
<properties>
18-
<java.version>21</java.version>
19-
<spring-cloud.version>2025.0.0</spring-cloud.version>
18+
<java.version>25</java.version>
19+
<spring-cloud.version>2025.1.0-RC1</spring-cloud.version>
20+
<spring-aop.version>4.0.0-M2</spring-aop.version>
2021
</properties>
2122

2223
<dependencies>
@@ -26,7 +27,7 @@
2627
</dependency>
2728
<dependency>
2829
<groupId>org.springframework.boot</groupId>
29-
<artifactId>spring-boot-starter-web</artifactId>
30+
<artifactId>spring-boot-starter-webmvc</artifactId>
3031
</dependency>
3132
<dependency>
3233
<groupId>org.springframework.boot</groupId>
@@ -108,6 +109,7 @@
108109
<dependency>
109110
<groupId>org.springframework.boot</groupId>
110111
<artifactId>spring-boot-starter-aop</artifactId>
112+
<version>${spring-aop.version}</version>
111113
</dependency>
112114

113115
<dependency>
@@ -121,9 +123,14 @@
121123
<scope>runtime</scope>
122124
<optional>true</optional>
123125
</dependency>
126+
<dependency>
127+
<groupId>org.springframework.boot</groupId>
128+
<artifactId>spring-boot-starter-actuator-test</artifactId>
129+
<scope>test</scope>
130+
</dependency>
124131
<dependency>
125132
<groupId>org.springframework.boot</groupId>
126-
<artifactId>spring-boot-starter-test</artifactId>
133+
<artifactId>spring-boot-starter-webmvc-test</artifactId>
127134
<scope>test</scope>
128135
</dependency>
129136
</dependencies>
@@ -149,12 +156,4 @@
149156
</plugins>
150157
</build>
151158

152-
<repositories>
153-
<repository>
154-
<id>spring-milestones</id>
155-
<name>Spring Milestones</name>
156-
<url>https://repo.spring.io/milestone</url>
157-
</repository>
158-
</repositories>
159-
160159
</project>

0 commit comments

Comments
 (0)