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:
parent
6e9091f686
commit
69fad866d9
@ -1,13 +0,0 @@
|
|||||||
*.class
|
|
||||||
|
|
||||||
#folders#
|
|
||||||
/target
|
|
||||||
/neoDb*
|
|
||||||
/data
|
|
||||||
/src/main/webapp/WEB-INF/classes
|
|
||||||
*/META-INF/*
|
|
||||||
|
|
||||||
# Packaged files #
|
|
||||||
*.jar
|
|
||||||
*.war
|
|
||||||
*.ear
|
|
@ -10,71 +10,20 @@
|
|||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-boot-2</artifactId>
|
<artifactId>parent-boot-3</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../../parent-boot-2</relativePath>
|
<relativePath>../../parent-boot-3</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<!-- Spring Boot 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>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-actuator</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>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.thoughtworks.xstream</groupId>
|
<groupId>com.thoughtworks.xstream</groupId>
|
||||||
@ -100,14 +49,15 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
<!-- test scoped -->
|
<!-- test scoped -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.mockito</groupId>
|
|
||||||
<artifactId>mockito-junit-jupiter</artifactId>
|
|
||||||
<version>${mockito.version}</version>
|
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.junit.vintage</groupId>
|
||||||
|
<artifactId>junit-vintage-engine</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
@ -120,14 +70,13 @@
|
|||||||
</resource>
|
</resource>
|
||||||
</resources>
|
</resources>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>${maven.version}</version>
|
|
||||||
<configuration>
|
|
||||||
<source>${source.version}</source>
|
|
||||||
<target>${target.version}</target>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
@ -254,16 +203,12 @@
|
|||||||
</profiles>
|
</profiles>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<xstream.version>1.4.9</xstream.version>
|
<xstream.version>1.4.20</xstream.version>
|
||||||
<!-- Maven plugins -->
|
<!-- Maven plugins -->
|
||||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||||
<findbugs-maven-plugin.version>3.0.4</findbugs-maven-plugin.version>
|
<findbugs-maven-plugin.version>3.0.4</findbugs-maven-plugin.version>
|
||||||
<!-- okhttp -->
|
<!-- okhttp -->
|
||||||
<com.squareup.okhttp3.version>4.12.0</com.squareup.okhttp3.version>
|
<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>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -7,7 +7,6 @@ import org.springframework.context.annotation.Bean;
|
|||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.DependsOn;
|
import org.springframework.context.annotation.DependsOn;
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
|
@ -2,40 +2,32 @@ package com.baeldung.resttemplate.web.handler;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import com.baeldung.resttemplate.web.exception.NotFoundException;
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.client.ClientHttpResponse;
|
import org.springframework.http.client.ClientHttpResponse;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.client.HttpClientErrorException;
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
import org.springframework.web.client.ResponseErrorHandler;
|
import org.springframework.web.client.ResponseErrorHandler;
|
||||||
|
|
||||||
|
import com.baeldung.resttemplate.web.exception.NotFoundException;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class RestTemplateResponseErrorHandler
|
public class RestTemplateResponseErrorHandler implements ResponseErrorHandler {
|
||||||
implements ResponseErrorHandler {
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasError(ClientHttpResponse httpResponse)
|
public boolean hasError(ClientHttpResponse httpResponse) throws IOException {
|
||||||
throws IOException {
|
return httpResponse.getStatusCode()
|
||||||
|
.is5xxServerError() || httpResponse.getStatusCode()
|
||||||
return (httpResponse
|
.is4xxClientError();
|
||||||
.getStatusCode()
|
|
||||||
.series() == HttpStatus.Series.CLIENT_ERROR || httpResponse
|
|
||||||
.getStatusCode()
|
|
||||||
.series() == HttpStatus.Series.SERVER_ERROR);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleError(ClientHttpResponse httpResponse)
|
public void handleError(ClientHttpResponse httpResponse) throws IOException {
|
||||||
throws IOException {
|
if (httpResponse.getStatusCode()
|
||||||
|
.is5xxServerError()) {
|
||||||
if (httpResponse
|
|
||||||
.getStatusCode()
|
|
||||||
.series() == HttpStatus.Series.SERVER_ERROR) {
|
|
||||||
//Handle SERVER_ERROR
|
//Handle SERVER_ERROR
|
||||||
throw new HttpClientErrorException(httpResponse.getStatusCode());
|
throw new HttpClientErrorException(httpResponse.getStatusCode());
|
||||||
} else if (httpResponse
|
} else if (httpResponse.getStatusCode()
|
||||||
.getStatusCode()
|
.is4xxClientError()) {
|
||||||
.series() == HttpStatus.Series.CLIENT_ERROR) {
|
|
||||||
//Handle CLIENT_ERROR
|
//Handle CLIENT_ERROR
|
||||||
if (httpResponse.getStatusCode() == HttpStatus.NOT_FOUND) {
|
if (httpResponse.getStatusCode() == HttpStatus.NOT_FOUND) {
|
||||||
throw new NotFoundException();
|
throw new NotFoundException();
|
||||||
|
@ -4,7 +4,7 @@ import java.util.Collection;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.baeldung.mock;
|
package com.baeldung.mock;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
@ -18,8 +20,6 @@ import com.baeldung.resttemplate.web.model.Employee;
|
|||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
public class EmployeeServiceUnitTest {
|
public class EmployeeServiceUnitTest {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(EmployeeServiceUnitTest.class);
|
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private RestTemplate restTemplate;
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
@ -34,7 +34,6 @@ public class EmployeeServiceUnitTest {
|
|||||||
|
|
||||||
Employee employee = empService.getEmployee("E001");
|
Employee employee = empService.getEmployee("E001");
|
||||||
|
|
||||||
Assertions.assertEquals(emp, employee);
|
assertEquals(emp, employee);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package com.baeldung.resttemplate;
|
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 com.baeldung.client.Consts.APPLICATION_PORT;
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
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.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
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;
|
import com.google.common.base.Charsets;
|
||||||
|
|
||||||
// This test needs RestTemplateConfigurationApplication to be up and running
|
// This test needs RestTemplateConfigurationApplication to be up and running
|
||||||
@ -62,7 +62,7 @@ public class RestTemplateBasicLiveTest {
|
|||||||
final RestTemplate template = new RestTemplate();
|
final RestTemplate template = new RestTemplate();
|
||||||
final ResponseEntity<String> response = template.getForEntity(fooResourceUrl + "/1", String.class);
|
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 root = mapper.readTree(response.getBody());
|
||||||
final JsonNode name = root.path("name");
|
final JsonNode name = root.path("name");
|
||||||
Assertions.assertNotNull(name.asText());
|
Assertions.assertNotNull(name.asText());
|
||||||
@ -243,7 +243,7 @@ public class RestTemplateBasicLiveTest {
|
|||||||
|
|
||||||
private String getBase64EncodedLogPass() {
|
private String getBase64EncodedLogPass() {
|
||||||
final String logPass = "user1:user1Pass";
|
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);
|
return new String(authHeaderBytes, Charsets.US_ASCII);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user