JAVA-30632 Upgrade spring-resttemplate module to Spring Boot 3 (#15713)

* JAVA-30632 upgrade Spring rest template module to Spring Boot 3

* JAVA-30632 fix broken tests
This commit is contained in:
Kasra Madadipouya 2024-01-23 16:54:18 +01:00 committed by GitHub
parent 6e9091f686
commit 69fad866d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 36 additions and 114 deletions

View File

@ -1,13 +0,0 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear

View File

@ -10,71 +10,20 @@
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<artifactId>parent-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
<relativePath>../../parent-boot-3</relativePath>
</parent>
<dependencies>
<!-- Spring Boot Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>au.com.dius</groupId>
<artifactId>pact-jvm-provider-junit5_2.12</artifactId>
<version>${pact.version}</version>
</dependency>
<dependency>
<groupId>au.com.dius</groupId>
<artifactId>pact-jvm-consumer-junit5_2.12</artifactId>
<version>${pact.version}</version>
<scope>test</scope>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
</dependency>
<!-- marshalling -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
@ -100,14 +49,15 @@
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
@ -120,14 +70,13 @@
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.version}</version>
<configuration>
<source>${source.version}</source>
<target>${target.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@ -254,16 +203,12 @@
</profiles>
<properties>
<xstream.version>1.4.9</xstream.version>
<xstream.version>1.4.20</xstream.version>
<!-- Maven plugins -->
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
<findbugs-maven-plugin.version>3.0.4</findbugs-maven-plugin.version>
<!-- okhttp -->
<com.squareup.okhttp3.version>4.12.0</com.squareup.okhttp3.version>
<pact.version>3.6.3</pact.version>
<source.version>1.8</source.version>
<target.version>1.8</target.version>
<maven.version>3.7.0</maven.version>
</properties>
</project>

View File

@ -7,7 +7,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.web.client.RestTemplate;
@Configuration
@EnableAutoConfiguration

View File

@ -2,40 +2,32 @@ package com.baeldung.resttemplate.web.handler;
import java.io.IOException;
import com.baeldung.resttemplate.web.exception.NotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.ResponseErrorHandler;
import com.baeldung.resttemplate.web.exception.NotFoundException;
@Component
public class RestTemplateResponseErrorHandler
implements ResponseErrorHandler {
public class RestTemplateResponseErrorHandler implements ResponseErrorHandler {
@Override
public boolean hasError(ClientHttpResponse httpResponse)
throws IOException {
return (httpResponse
.getStatusCode()
.series() == HttpStatus.Series.CLIENT_ERROR || httpResponse
.getStatusCode()
.series() == HttpStatus.Series.SERVER_ERROR);
public boolean hasError(ClientHttpResponse httpResponse) throws IOException {
return httpResponse.getStatusCode()
.is5xxServerError() || httpResponse.getStatusCode()
.is4xxClientError();
}
@Override
public void handleError(ClientHttpResponse httpResponse)
throws IOException {
if (httpResponse
.getStatusCode()
.series() == HttpStatus.Series.SERVER_ERROR) {
public void handleError(ClientHttpResponse httpResponse) throws IOException {
if (httpResponse.getStatusCode()
.is5xxServerError()) {
//Handle SERVER_ERROR
throw new HttpClientErrorException(httpResponse.getStatusCode());
} else if (httpResponse
.getStatusCode()
.series() == HttpStatus.Series.CLIENT_ERROR) {
} else if (httpResponse.getStatusCode()
.is4xxClientError()) {
//Handle CLIENT_ERROR
if (httpResponse.getStatusCode() == HttpStatus.NOT_FOUND) {
throw new NotFoundException();

View File

@ -4,7 +4,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;

View File

@ -1,5 +1,7 @@
package com.baeldung.mock;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ -18,8 +20,6 @@ import com.baeldung.resttemplate.web.model.Employee;
@ExtendWith(MockitoExtension.class)
public class EmployeeServiceUnitTest {
private static final Logger logger = LoggerFactory.getLogger(EmployeeServiceUnitTest.class);
@Mock
private RestTemplate restTemplate;
@ -34,7 +34,6 @@ public class EmployeeServiceUnitTest {
Employee employee = empService.getEmployee("E001");
Assertions.assertEquals(emp, employee);
assertEquals(emp, employee);
}
}

View File

@ -1,6 +1,6 @@
package com.baeldung.resttemplate;
import static org.apache.commons.codec.binary.Base64.encodeBase64;
import java.util.Base64;
import static com.baeldung.client.Consts.APPLICATION_PORT;
import static org.junit.jupiter.api.Assertions.fail;
@ -32,7 +32,7 @@ import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.springframework.web.client.RestTemplate;
import com.google.common.base.Charsets;
// This test needs RestTemplateConfigurationApplication to be up and running
@ -62,7 +62,7 @@ public class RestTemplateBasicLiveTest {
final RestTemplate template = new RestTemplate();
final ResponseEntity<String> response = template.getForEntity(fooResourceUrl + "/1", String.class);
final ObjectMapper mapper = new XmlMapper();
final ObjectMapper mapper = new ObjectMapper();
final JsonNode root = mapper.readTree(response.getBody());
final JsonNode name = root.path("name");
Assertions.assertNotNull(name.asText());
@ -243,7 +243,7 @@ public class RestTemplateBasicLiveTest {
private String getBase64EncodedLogPass() {
final String logPass = "user1:user1Pass";
final byte[] authHeaderBytes = encodeBase64(logPass.getBytes(Charsets.US_ASCII));
final byte[] authHeaderBytes = Base64.getEncoder().encode(logPass.getBytes(Charsets.US_ASCII));
return new String(authHeaderBytes, Charsets.US_ASCII);
}