Skip to content

Commit c30aff7

Browse files
AndreiBranzaAndrei Branza
andauthored
BAEL-5775 | Add classes and tests relevant to BAEL-5775 article. (#18674)
* BAEL-5775 | Add classes and tests relevant to BAEL-5775 article. * BAEL-5775 | Updates according to editor feedback * BAEL-5775 | Removed some not needed code. * BAEL-5775 | Update spring-webflux and reactor versions in pom.xml * BAEL-5775 | Refactor code for improved readability and consistency * Add method to fetch users list using existing ParameterizedTypeReference * BAEL-5775 | Update Java version in pom.xml to 17 --------- Co-authored-by: Andrei Branza <[email protected]>
1 parent c273c8e commit c30aff7

File tree

9 files changed

+566
-0
lines changed

9 files changed

+566
-0
lines changed

spring-core-4/pom.xml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@
55
<modelVersion>4.0.0</modelVersion>
66
<artifactId>spring-core-4</artifactId>
77
<name>spring-core-4</name>
8+
<build>
9+
<plugins>
10+
<plugin>
11+
<groupId>org.apache.maven.plugins</groupId>
12+
<artifactId>maven-compiler-plugin</artifactId>
13+
<configuration>
14+
<source>17</source>
15+
<target>17</target>
16+
</configuration>
17+
</plugin>
18+
</plugins>
19+
</build>
820

921
<parent>
1022
<groupId>com.baeldung</groupId>
@@ -70,13 +82,56 @@
7082
<artifactId>commons-text</artifactId>
7183
<version>${commons-text.version}</version>
7284
</dependency>
85+
<dependency>
86+
<groupId>org.springframework</groupId>
87+
<artifactId>spring-webflux</artifactId>
88+
<version>${spring-webflux.version}</version>
89+
</dependency>
90+
<dependency>
91+
<groupId>org.mockito</groupId>
92+
<artifactId>mockito-junit-jupiter</artifactId>
93+
<version>${mockito.version}</version>
94+
<scope>test</scope>
95+
</dependency>
96+
<dependency>
97+
<groupId>io.projectreactor</groupId>
98+
<artifactId>reactor-test</artifactId>
99+
<version>${reactor.version}</version>
100+
<scope>test</scope>
101+
</dependency>
102+
<dependency>
103+
<groupId>com.github.tomakehurst</groupId>
104+
<artifactId>wiremock-jre8-standalone</artifactId>
105+
<version>${wiremock.version}</version>
106+
<scope>test</scope>
107+
</dependency>
108+
<dependency>
109+
<groupId>com.fasterxml.jackson.core</groupId>
110+
<artifactId>jackson-databind</artifactId>
111+
<version>${jackson.version}</version>
112+
</dependency>
113+
<dependency>
114+
<groupId>com.fasterxml.jackson.core</groupId>
115+
<artifactId>jackson-core</artifactId>
116+
<version>${jackson.version}</version>
117+
</dependency>
118+
<dependency>
119+
<groupId>com.fasterxml.jackson.core</groupId>
120+
<artifactId>jackson-annotations</artifactId>
121+
<version>${jackson.version}</version>
122+
</dependency>
73123
</dependencies>
74124

75125
<properties>
76126
<awaitility.version>4.0.2</awaitility.version>
77127
<jakarta-servlet.version>6.1.0</jakarta-servlet.version>
78128
<jakarta-annotation.version>3.0.0</jakarta-annotation.version>
79129
<commons-text.version>1.10.0</commons-text.version>
130+
<spring-webflux.version>6.2.9</spring-webflux.version>
131+
<mockito.version>5.18.0</mockito.version>
132+
<reactor.version>3.7.8</reactor.version>
133+
<wiremock.version>2.35.0</wiremock.version>
134+
<jackson.version>2.19.1</jackson.version>
80135
</properties>
81136

82137
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.parametrizedtypereference;
2+
3+
public class ApiException extends RuntimeException {
4+
5+
public ApiException(String message) {
6+
super(message);
7+
}
8+
9+
public ApiException(String message, Throwable cause) {
10+
super(message, cause);
11+
}
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.parametrizedtypereference;
2+
3+
public record ApiResponse<T>(boolean success, String message, T data) {
4+
5+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.baeldung.parametrizedtypereference;
2+
3+
import static com.baeldung.parametrizedtypereference.TypeReferences.USER_LIST;
4+
5+
import java.util.List;
6+
7+
import org.springframework.core.ParameterizedTypeReference;
8+
import org.springframework.http.HttpMethod;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.stereotype.Service;
11+
import org.springframework.web.client.RestTemplate;
12+
13+
@Service
14+
public class ApiService {
15+
16+
private final RestTemplate restTemplate;
17+
private final String baseUrl;
18+
19+
public ApiService(RestTemplate restTemplate, String baseUrl) {
20+
this.restTemplate = restTemplate;
21+
this.baseUrl = baseUrl;
22+
}
23+
24+
public List<User> fetchUserList() {
25+
ParameterizedTypeReference<List<User>> typeRef = new ParameterizedTypeReference<List<User>>() {
26+
};
27+
28+
ResponseEntity<List<User>> response = restTemplate.exchange(baseUrl + "/api/users", HttpMethod.GET, null, typeRef);
29+
30+
return response.getBody();
31+
}
32+
33+
public List<User> fetchUsersWrongApproach() {
34+
ResponseEntity<List> response = restTemplate.getForEntity(baseUrl + "/api/users", List.class);
35+
36+
return (List<User>) response.getBody();
37+
}
38+
39+
public List<User> fetchUsersCorrectApproach() {
40+
ParameterizedTypeReference<List<User>> typeRef = new ParameterizedTypeReference<List<User>>() {
41+
};
42+
43+
ResponseEntity<List<User>> response = restTemplate.exchange(baseUrl + "/api/users", HttpMethod.GET, null, typeRef);
44+
45+
return response.getBody();
46+
}
47+
48+
public User fetchUser(Long id) {
49+
return restTemplate.getForObject(baseUrl + "/api/users/" + id, User.class);
50+
}
51+
52+
public User[] fetchUsersArray() {
53+
return restTemplate.getForObject(baseUrl + "/api/users", User[].class);
54+
}
55+
56+
public List<User> fetchUsersList() {
57+
ParameterizedTypeReference<List<User>> typeRef = new ParameterizedTypeReference<List<User>>() {
58+
};
59+
60+
ResponseEntity<List<User>> response = restTemplate.exchange(baseUrl + "/api/users", HttpMethod.GET, null, typeRef);
61+
62+
return response.getBody();
63+
}
64+
65+
public List<User> fetchUsersListWithExistingReference() {
66+
ResponseEntity<List<User>> response = restTemplate.exchange(baseUrl + "/api/users", HttpMethod.GET, null, USER_LIST);
67+
68+
return response.getBody();
69+
}
70+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.parametrizedtypereference;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import org.springframework.core.ParameterizedTypeReference;
7+
import org.springframework.stereotype.Service;
8+
import org.springframework.web.reactive.function.client.WebClient;
9+
10+
import reactor.core.publisher.Mono;
11+
12+
@Service
13+
public class ReactiveApiService {
14+
15+
private final WebClient webClient;
16+
17+
public ReactiveApiService(String baseUrl) {
18+
this.webClient = WebClient.builder()
19+
.baseUrl(baseUrl)
20+
.build();
21+
}
22+
23+
public Mono<Map<String, List<User>>> fetchUsersByDepartment() {
24+
ParameterizedTypeReference<Map<String, List<User>>> typeRef = new ParameterizedTypeReference<Map<String, List<User>>>() {
25+
};
26+
27+
return webClient.get()
28+
.uri("/users/by-department")
29+
.retrieve()
30+
.bodyToMono(typeRef);
31+
}
32+
33+
public Mono<ApiResponse<List<User>>> fetchUsersWithWrapper() {
34+
ParameterizedTypeReference<ApiResponse<List<User>>> typeRef = new ParameterizedTypeReference<ApiResponse<List<User>>>() {
35+
};
36+
37+
return webClient.get()
38+
.uri("/users/wrapped")
39+
.retrieve()
40+
.bodyToMono(typeRef);
41+
}
42+
43+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.parametrizedtypereference;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import org.springframework.core.ParameterizedTypeReference;
7+
8+
public class TypeReferences {
9+
10+
public static final ParameterizedTypeReference<List<User>> USER_LIST = new ParameterizedTypeReference<List<User>>() {
11+
};
12+
13+
public static final ParameterizedTypeReference<Map<String, List<User>>> USER_MAP = new ParameterizedTypeReference<Map<String, List<User>>>() {
14+
};
15+
16+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.parametrizedtypereference;
2+
3+
public class User {
4+
5+
private Long id;
6+
private String name;
7+
private String email;
8+
private String department;
9+
10+
public User() {
11+
}
12+
13+
public User(Long id, String name, String email, String department) {
14+
this.id = id;
15+
this.name = name;
16+
this.email = email;
17+
this.department = department;
18+
}
19+
20+
// Getters and setters
21+
public Long getId() {
22+
return id;
23+
}
24+
25+
public void setId(Long id) {
26+
this.id = id;
27+
}
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
37+
public String getEmail() {
38+
return email;
39+
}
40+
41+
public void setEmail(String email) {
42+
this.email = email;
43+
}
44+
45+
public String getDepartment() {
46+
return department;
47+
}
48+
49+
public void setDepartment(String department) {
50+
this.department = department;
51+
}
52+
}

0 commit comments

Comments
 (0)